3 Broadcom B43 wireless driver
7 Copyright (c) 2005-2008 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.
32 #include <linux/delay.h>
35 static void b43_pio_rx_work(struct work_struct *work);
38 static u16 generate_cookie(struct b43_pio_txqueue *q,
39 struct b43_pio_txpacket *pack)
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.
51 cookie = (((u16)q->index + 1) << 12);
52 cookie |= pack->index;
58 struct b43_pio_txqueue * parse_cookie(struct b43_wldev *dev,
60 struct b43_pio_txpacket **pack)
62 struct b43_pio *pio = &dev->pio;
63 struct b43_pio_txqueue *q = NULL;
64 unsigned int pack_index;
66 switch (cookie & 0xF000) {
68 q = pio->tx_queue_AC_BK;
71 q = pio->tx_queue_AC_BE;
74 q = pio->tx_queue_AC_VI;
77 q = pio->tx_queue_AC_VO;
80 q = pio->tx_queue_mcast;
85 pack_index = (cookie & 0x0FFF);
86 if (B43_WARN_ON(pack_index >= ARRAY_SIZE(q->packets)))
88 *pack = &q->packets[pack_index];
93 static u16 index_to_pioqueue_base(struct b43_wldev *dev,
96 static const u16 bases[] = {
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,
115 if (dev->dev->id.revision >= 11) {
116 B43_WARN_ON(index >= ARRAY_SIZE(bases_rev11));
117 return bases_rev11[index];
119 B43_WARN_ON(index >= ARRAY_SIZE(bases));
123 static u16 pio_txqueue_offset(struct b43_wldev *dev)
125 if (dev->dev->id.revision >= 11)
130 static u16 pio_rxqueue_offset(struct b43_wldev *dev)
132 if (dev->dev->id.revision >= 11)
137 static struct b43_pio_txqueue * b43_setup_pioqueue_tx(struct b43_wldev *dev,
140 struct b43_pio_txqueue *q;
141 struct b43_pio_txpacket *p;
144 q = kzalloc(sizeof(*q), GFP_KERNEL);
147 spin_lock_init(&q->lock);
149 q->rev = dev->dev->id.revision;
150 q->mmio_base = index_to_pioqueue_base(dev, index) +
151 pio_txqueue_offset(dev);
154 q->free_packet_slots = B43_PIO_MAX_NR_TXPACKETS;
156 q->buffer_size = 1920; //FIXME this constant is wrong.
158 q->buffer_size = b43_piotx_read16(q, B43_PIO_TXQBUFSIZE);
159 q->buffer_size -= 80;
162 INIT_LIST_HEAD(&q->packets_list);
163 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
164 p = &(q->packets[i]);
165 INIT_LIST_HEAD(&p->list);
168 list_add(&p->list, &q->packets_list);
174 static struct b43_pio_rxqueue * b43_setup_pioqueue_rx(struct b43_wldev *dev,
177 struct b43_pio_rxqueue *q;
179 q = kzalloc(sizeof(*q), GFP_KERNEL);
182 spin_lock_init(&q->lock);
184 q->rev = dev->dev->id.revision;
185 q->mmio_base = index_to_pioqueue_base(dev, index) +
186 pio_rxqueue_offset(dev);
187 INIT_WORK(&q->rx_work, b43_pio_rx_work);
189 /* Enable Direct FIFO RX (PIO) on the engine. */
190 b43_dma_direct_fifo_rx(dev, index, 1);
195 static void b43_pio_cancel_tx_packets(struct b43_pio_txqueue *q)
197 struct b43_pio_txpacket *pack;
200 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
201 pack = &(q->packets[i]);
203 dev_kfree_skb_any(pack->skb);
209 static void b43_destroy_pioqueue_tx(struct b43_pio_txqueue *q,
214 b43_pio_cancel_tx_packets(q);
218 static void b43_destroy_pioqueue_rx(struct b43_pio_rxqueue *q,
226 #define destroy_queue_tx(pio, queue) do { \
227 b43_destroy_pioqueue_tx((pio)->queue, __stringify(queue)); \
228 (pio)->queue = NULL; \
231 #define destroy_queue_rx(pio, queue) do { \
232 b43_destroy_pioqueue_rx((pio)->queue, __stringify(queue)); \
233 (pio)->queue = NULL; \
236 void b43_pio_free(struct b43_wldev *dev)
240 if (!b43_using_pio_transfers(dev))
244 destroy_queue_rx(pio, rx_queue);
245 destroy_queue_tx(pio, tx_queue_mcast);
246 destroy_queue_tx(pio, tx_queue_AC_VO);
247 destroy_queue_tx(pio, tx_queue_AC_VI);
248 destroy_queue_tx(pio, tx_queue_AC_BE);
249 destroy_queue_tx(pio, tx_queue_AC_BK);
252 void b43_pio_stop(struct b43_wldev *dev)
254 if (!b43_using_pio_transfers(dev))
256 cancel_work_sync(&dev->pio.rx_queue->rx_work);
259 int b43_pio_init(struct b43_wldev *dev)
261 struct b43_pio *pio = &dev->pio;
264 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
266 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RXPADOFF, 0);
268 pio->tx_queue_AC_BK = b43_setup_pioqueue_tx(dev, 0);
269 if (!pio->tx_queue_AC_BK)
272 pio->tx_queue_AC_BE = b43_setup_pioqueue_tx(dev, 1);
273 if (!pio->tx_queue_AC_BE)
276 pio->tx_queue_AC_VI = b43_setup_pioqueue_tx(dev, 2);
277 if (!pio->tx_queue_AC_VI)
280 pio->tx_queue_AC_VO = b43_setup_pioqueue_tx(dev, 3);
281 if (!pio->tx_queue_AC_VO)
284 pio->tx_queue_mcast = b43_setup_pioqueue_tx(dev, 4);
285 if (!pio->tx_queue_mcast)
288 pio->rx_queue = b43_setup_pioqueue_rx(dev, 0);
290 goto err_destroy_mcast;
292 b43dbg(dev->wl, "PIO initialized\n");
298 destroy_queue_tx(pio, tx_queue_mcast);
300 destroy_queue_tx(pio, tx_queue_AC_VO);
302 destroy_queue_tx(pio, tx_queue_AC_VI);
304 destroy_queue_tx(pio, tx_queue_AC_BE);
306 destroy_queue_tx(pio, tx_queue_AC_BK);
310 /* Static mapping of mac80211's queues (priorities) to b43 PIO queues. */
311 static struct b43_pio_txqueue * select_queue_by_priority(struct b43_wldev *dev,
314 struct b43_pio_txqueue *q;
316 if (b43_modparam_qos) {
317 /* 0 = highest priority */
318 switch (queue_prio) {
323 q = dev->pio.tx_queue_AC_VO;
326 q = dev->pio.tx_queue_AC_VI;
329 q = dev->pio.tx_queue_AC_BE;
332 q = dev->pio.tx_queue_AC_BK;
336 q = dev->pio.tx_queue_AC_BE;
341 static inline void tx_write_2byte_queue(struct b43_pio_txqueue *q,
344 unsigned int data_len)
346 const u8 *data = _data;
350 *ctl |= B43_PIO_TXCTL_WRITELO | B43_PIO_TXCTL_WRITEHI;
351 b43_piotx_write16(q, B43_PIO_TXCTL, *ctl);
352 for (i = 0; i < data_len; i += 2) {
354 if (i + 1 < data_len) {
355 value |= (u16)(data[i + 1]) << 8;
357 *ctl &= ~B43_PIO_TXCTL_WRITEHI;
358 b43_piotx_write16(q, B43_PIO_TXCTL, *ctl);
360 b43_piotx_write16(q, B43_PIO_TXDATA, value);
364 static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
365 const u8 *hdr, unsigned int hdrlen)
367 struct b43_pio_txqueue *q = pack->queue;
368 const char *frame = pack->skb->data;
369 unsigned int frame_len = pack->skb->len;
372 ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
373 ctl |= B43_PIO_TXCTL_FREADY;
374 ctl &= ~B43_PIO_TXCTL_EOF;
376 /* Transfer the header data. */
377 tx_write_2byte_queue(q, &ctl, hdr, hdrlen);
378 /* Transfer the frame data. */
379 tx_write_2byte_queue(q, &ctl, frame, frame_len);
381 ctl |= B43_PIO_TXCTL_EOF;
382 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
385 static inline void tx_write_4byte_queue(struct b43_pio_txqueue *q,
388 unsigned int data_len)
390 const u8 *data = _data;
393 bool ctl_changed = 0;
395 *ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
396 B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
397 b43_piotx_write32(q, B43_PIO8_TXCTL, *ctl);
398 for (i = 0; i < data_len; i += 4) {
400 if (i + 1 < data_len) {
401 value |= (u32)(data[i + 1]) << 8;
403 *ctl &= ~B43_PIO8_TXCTL_8_15;
406 if (i + 2 < data_len) {
407 value |= (u32)(data[i + 2]) << 16;
409 *ctl &= ~B43_PIO8_TXCTL_16_23;
412 if (i + 3 < data_len) {
413 value |= (u32)(data[i + 3]) << 24;
415 *ctl &= ~B43_PIO8_TXCTL_24_31;
419 b43_piotx_write32(q, B43_PIO8_TXCTL, *ctl);
420 b43_piotx_write32(q, B43_PIO8_TXDATA, value);
424 static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
425 const u8 *hdr, unsigned int hdrlen)
427 struct b43_pio_txqueue *q = pack->queue;
428 const char *frame = pack->skb->data;
429 unsigned int frame_len = pack->skb->len;
432 ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
433 ctl |= B43_PIO8_TXCTL_FREADY;
434 ctl &= ~B43_PIO8_TXCTL_EOF;
436 /* Transfer the header data. */
437 tx_write_4byte_queue(q, &ctl, hdr, hdrlen);
438 /* Transfer the frame data. */
439 tx_write_4byte_queue(q, &ctl, frame, frame_len);
441 ctl |= B43_PIO8_TXCTL_EOF;
442 b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
445 static int pio_tx_frame(struct b43_pio_txqueue *q,
447 struct ieee80211_tx_control *ctl)
449 struct b43_pio_txpacket *pack;
450 struct b43_txhdr txhdr;
455 B43_WARN_ON(list_empty(&q->packets_list));
456 pack = list_entry(q->packets_list.next,
457 struct b43_pio_txpacket, list);
458 memset(&pack->txstat, 0, sizeof(pack->txstat));
459 memcpy(&pack->txstat.control, ctl, sizeof(*ctl));
461 cookie = generate_cookie(q, pack);
462 hdrlen = b43_txhdr_size(q->dev);
463 err = b43_generate_txhdr(q->dev, (u8 *)&txhdr, skb->data,
464 skb->len, ctl, cookie);
468 if (ctl->flags & IEEE80211_TXCTL_SEND_AFTER_DTIM) {
469 /* Tell the firmware about the cookie of the last
470 * mcast frame, so it can clear the more-data bit in it. */
471 b43_shm_write16(q->dev, B43_SHM_SHARED,
472 B43_SHM_SH_MCASTCOOKIE, cookie);
477 pio_tx_frame_4byte_queue(pack, (const u8 *)&txhdr, hdrlen);
479 pio_tx_frame_2byte_queue(pack, (const u8 *)&txhdr, hdrlen);
481 /* Remove it from the list of available packet slots.
482 * It will be put back when we receive the status report. */
483 list_del(&pack->list);
485 /* Update the queue statistics. */
486 q->buffer_used += roundup(skb->len + hdrlen, 4);
487 q->free_packet_slots -= 1;
492 int b43_pio_tx(struct b43_wldev *dev,
493 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
495 struct b43_pio_txqueue *q;
496 struct ieee80211_hdr *hdr;
498 unsigned int hdrlen, total_len;
501 hdr = (struct ieee80211_hdr *)skb->data;
502 if (ctl->flags & IEEE80211_TXCTL_SEND_AFTER_DTIM) {
503 /* The multicast queue will be sent after the DTIM. */
504 q = dev->pio.tx_queue_mcast;
505 /* Set the frame More-Data bit. Ucode will clear it
506 * for us on the last frame. */
507 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
509 /* Decide by priority where to put this frame. */
510 q = select_queue_by_priority(dev, ctl->queue);
513 spin_lock_irqsave(&q->lock, flags);
515 hdrlen = b43_txhdr_size(dev);
516 total_len = roundup(skb->len + hdrlen, 4);
518 if (unlikely(total_len > q->buffer_size)) {
520 b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
523 if (unlikely(q->free_packet_slots == 0)) {
525 b43warn(dev->wl, "PIO: TX packet overflow.\n");
528 B43_WARN_ON(q->buffer_used > q->buffer_size);
530 if (total_len > (q->buffer_size - q->buffer_used)) {
531 /* Not enough memory on the queue. */
533 ieee80211_stop_queue(dev->wl->hw, ctl->queue);
538 /* Assign the queue number to the ring (if not already done before)
539 * so TX status handling can use it. The mac80211-queue to b43-queue
540 * mapping is static, so we don't need to store it per frame. */
541 q->queue_prio = ctl->queue;
543 err = pio_tx_frame(q, skb, ctl);
544 if (unlikely(err == -ENOKEY)) {
545 /* Drop this packet, as we don't have the encryption key
546 * anymore and must not transmit it unencrypted. */
547 dev_kfree_skb_any(skb);
552 b43err(dev->wl, "PIO transmission failure\n");
557 B43_WARN_ON(q->buffer_used > q->buffer_size);
558 if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
559 (q->free_packet_slots == 0)) {
560 /* The queue is full. */
561 ieee80211_stop_queue(dev->wl->hw, ctl->queue);
566 spin_unlock_irqrestore(&q->lock, flags);
571 /* Called with IRQs disabled. */
572 void b43_pio_handle_txstatus(struct b43_wldev *dev,
573 const struct b43_txstatus *status)
575 struct b43_pio_txqueue *q;
576 struct b43_pio_txpacket *pack = NULL;
577 unsigned int total_len;
579 q = parse_cookie(dev, status->cookie, &pack);
584 spin_lock(&q->lock); /* IRQs are already disabled. */
586 b43_fill_txstatus_report(&(pack->txstat), status);
588 total_len = pack->skb->len + b43_txhdr_size(dev);
589 total_len = roundup(total_len, 4);
590 q->buffer_used -= total_len;
591 q->free_packet_slots += 1;
593 ieee80211_tx_status_irqsafe(dev->wl->hw, pack->skb,
596 list_add(&pack->list, &q->packets_list);
599 ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
603 spin_unlock(&q->lock);
606 void b43_pio_get_tx_stats(struct b43_wldev *dev,
607 struct ieee80211_tx_queue_stats *stats)
609 const int nr_queues = dev->wl->hw->queues;
610 struct b43_pio_txqueue *q;
611 struct ieee80211_tx_queue_stats_data *data;
615 for (i = 0; i < nr_queues; i++) {
616 data = &(stats->data[i]);
617 q = select_queue_by_priority(dev, i);
619 spin_lock_irqsave(&q->lock, flags);
620 data->len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
621 data->limit = B43_PIO_MAX_NR_TXPACKETS;
622 data->count = q->nr_tx_packets;
623 spin_unlock_irqrestore(&q->lock, flags);
627 /* Returns whether we should fetch another frame. */
628 static bool pio_rx_frame(struct b43_pio_rxqueue *q)
630 struct b43_rxhdr_fw4 rxhdr;
633 unsigned int i, padding;
635 const char *err_msg = NULL;
637 memset(&rxhdr, 0, sizeof(rxhdr));
639 /* Check if we have data and wait for it to get ready. */
643 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
644 if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
646 b43_piorx_write32(q, B43_PIO8_RXCTL,
647 B43_PIO8_RXCTL_FRAMERDY);
648 for (i = 0; i < 10; i++) {
649 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
650 if (ctl & B43_PIO8_RXCTL_DATARDY)
657 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
658 if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
660 b43_piorx_write16(q, B43_PIO_RXCTL,
661 B43_PIO_RXCTL_FRAMERDY);
662 for (i = 0; i < 10; i++) {
663 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
664 if (ctl & B43_PIO_RXCTL_DATARDY)
669 b43dbg(q->dev->wl, "PIO RX timed out\n");
673 /* Get the preamble (RX header) */
675 u32 *preamble = (u32 *)&rxhdr;
678 for (i = 0; i < sizeof(rxhdr); i += 4) {
679 value = b43_piorx_read32(q, B43_PIO8_RXDATA);
680 preamble[i / 4] = cpu_to_le32(value);
683 u16 *preamble = (u16 *)&rxhdr;
686 for (i = 0; i < sizeof(rxhdr); i += 2) {
687 value = b43_piorx_read16(q, B43_PIO_RXDATA);
688 preamble[i / 2] = cpu_to_le16(value);
692 len = le16_to_cpu(rxhdr.frame_len);
693 if (unlikely(len > 0x700)) {
694 err_msg = "len > 0x700";
697 if (unlikely(len == 0)) {
698 err_msg = "len == 0";
702 macstat = le32_to_cpu(rxhdr.mac_status);
703 if (macstat & B43_RX_MAC_FCSERR) {
704 if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
705 /* Drop frames with failed FCS. */
706 err_msg = "Frame FCS error";
711 /* We always pad 2 bytes, as that's what upstream code expects
712 * due to the RX-header being 30 bytes. In case the frame is
713 * unaligned, we pad another 2 bytes. */
714 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
715 skb = dev_alloc_skb(len + padding + 2);
716 if (unlikely(!skb)) {
717 err_msg = "Out of memory";
721 skb_put(skb, len + padding);
725 for (i = padding; i < len + padding; i += 4) {
726 value = b43_piorx_read32(q, B43_PIO8_RXDATA);
727 skb->data[i] = value;
728 if ((i + 1) < (len + padding))
729 skb->data[i + 1] = value >> 8;
730 if ((i + 2) < (len + padding))
731 skb->data[i + 2] = value >> 16;
732 if ((i + 3) < (len + padding))
733 skb->data[i + 3] = value >> 24;
738 for (i = padding; i < len + padding; i += 2) {
739 value = b43_piorx_read16(q, B43_PIO_RXDATA);
740 skb->data[i] = value;
741 if ((i + 1) < (len + padding))
742 skb->data[i + 1] = value >> 8;
746 b43_rx(q->dev, skb, &rxhdr);
752 b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
753 b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
757 /* RX workqueue. We can sleep, yay! */
758 static void b43_pio_rx_work(struct work_struct *work)
760 struct b43_pio_rxqueue *q = container_of(work, struct b43_pio_rxqueue,
762 unsigned int budget = 50;
766 spin_lock_irq(&q->lock);
767 stop = (pio_rx_frame(q) == 0);
768 spin_unlock_irq(&q->lock);
775 /* Called with IRQs disabled. */
776 void b43_pio_rx(struct b43_pio_rxqueue *q)
778 /* Due to latency issues we must run the RX path in
779 * a workqueue to be able to schedule between packets. */
780 queue_work(q->dev->wl->hw->workqueue, &q->rx_work);
783 static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
787 spin_lock_irqsave(&q->lock, flags);
789 b43_piotx_write32(q, B43_PIO8_TXCTL,
790 b43_piotx_read32(q, B43_PIO8_TXCTL)
791 | B43_PIO8_TXCTL_SUSPREQ);
793 b43_piotx_write16(q, B43_PIO_TXCTL,
794 b43_piotx_read16(q, B43_PIO_TXCTL)
795 | B43_PIO_TXCTL_SUSPREQ);
797 spin_unlock_irqrestore(&q->lock, flags);
800 static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
804 spin_lock_irqsave(&q->lock, flags);
806 b43_piotx_write32(q, B43_PIO8_TXCTL,
807 b43_piotx_read32(q, B43_PIO8_TXCTL)
808 & ~B43_PIO8_TXCTL_SUSPREQ);
810 b43_piotx_write16(q, B43_PIO_TXCTL,
811 b43_piotx_read16(q, B43_PIO_TXCTL)
812 & ~B43_PIO_TXCTL_SUSPREQ);
814 spin_unlock_irqrestore(&q->lock, flags);
817 void b43_pio_tx_suspend(struct b43_wldev *dev)
819 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
820 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
821 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
822 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
823 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
824 b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
827 void b43_pio_tx_resume(struct b43_wldev *dev)
829 b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
830 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
831 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
832 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
833 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
834 b43_power_saving_ctl_bits(dev, 0);