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 u16 tx_write_2byte_queue(struct b43_pio_txqueue *q,
344 unsigned int data_len)
346 struct b43_wldev *dev = q->dev;
347 const u8 *data = _data;
349 ctl |= B43_PIO_TXCTL_WRITELO | B43_PIO_TXCTL_WRITEHI;
350 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
352 ssb_block_write(dev->dev, data, (data_len & ~1),
353 q->mmio_base + B43_PIO_TXDATA,
356 /* Write the last byte. */
357 ctl &= ~B43_PIO_TXCTL_WRITEHI;
358 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
359 b43_piotx_write16(q, B43_PIO_TXDATA, data[data_len - 1]);
365 static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
366 const u8 *hdr, unsigned int hdrlen)
368 struct b43_pio_txqueue *q = pack->queue;
369 const char *frame = pack->skb->data;
370 unsigned int frame_len = pack->skb->len;
373 ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
374 ctl |= B43_PIO_TXCTL_FREADY;
375 ctl &= ~B43_PIO_TXCTL_EOF;
377 /* Transfer the header data. */
378 ctl = tx_write_2byte_queue(q, ctl, hdr, hdrlen);
379 /* Transfer the frame data. */
380 ctl = tx_write_2byte_queue(q, ctl, frame, frame_len);
382 ctl |= B43_PIO_TXCTL_EOF;
383 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
386 static u32 tx_write_4byte_queue(struct b43_pio_txqueue *q,
389 unsigned int data_len)
391 struct b43_wldev *dev = q->dev;
392 const u8 *data = _data;
394 ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
395 B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
396 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
398 ssb_block_write(dev->dev, data, (data_len & ~3),
399 q->mmio_base + B43_PIO8_TXDATA,
404 /* Write the last few bytes. */
405 ctl &= ~(B43_PIO8_TXCTL_8_15 | B43_PIO8_TXCTL_16_23 |
406 B43_PIO8_TXCTL_24_31);
407 data = &(data[data_len - 1]);
408 switch (data_len & 3) {
410 ctl |= B43_PIO8_TXCTL_16_23;
411 value |= (u32)(*data) << 16;
414 ctl |= B43_PIO8_TXCTL_8_15;
415 value |= (u32)(*data) << 8;
418 value |= (u32)(*data);
420 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
421 b43_piotx_write32(q, B43_PIO8_TXDATA, value);
427 static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
428 const u8 *hdr, unsigned int hdrlen)
430 struct b43_pio_txqueue *q = pack->queue;
431 const char *frame = pack->skb->data;
432 unsigned int frame_len = pack->skb->len;
435 ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
436 ctl |= B43_PIO8_TXCTL_FREADY;
437 ctl &= ~B43_PIO8_TXCTL_EOF;
439 /* Transfer the header data. */
440 ctl = tx_write_4byte_queue(q, ctl, hdr, hdrlen);
441 /* Transfer the frame data. */
442 ctl = tx_write_4byte_queue(q, ctl, frame, frame_len);
444 ctl |= B43_PIO8_TXCTL_EOF;
445 b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
448 static int pio_tx_frame(struct b43_pio_txqueue *q,
451 struct b43_pio_txpacket *pack;
452 struct b43_txhdr txhdr;
456 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
458 B43_WARN_ON(list_empty(&q->packets_list));
459 pack = list_entry(q->packets_list.next,
460 struct b43_pio_txpacket, list);
462 cookie = generate_cookie(q, pack);
463 hdrlen = b43_txhdr_size(q->dev);
464 err = b43_generate_txhdr(q->dev, (u8 *)&txhdr, skb->data,
465 skb->len, info, cookie);
469 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
470 /* Tell the firmware about the cookie of the last
471 * mcast frame, so it can clear the more-data bit in it. */
472 b43_shm_write16(q->dev, B43_SHM_SHARED,
473 B43_SHM_SH_MCASTCOOKIE, cookie);
478 pio_tx_frame_4byte_queue(pack, (const u8 *)&txhdr, hdrlen);
480 pio_tx_frame_2byte_queue(pack, (const u8 *)&txhdr, hdrlen);
482 /* Remove it from the list of available packet slots.
483 * It will be put back when we receive the status report. */
484 list_del(&pack->list);
486 /* Update the queue statistics. */
487 q->buffer_used += roundup(skb->len + hdrlen, 4);
488 q->free_packet_slots -= 1;
493 int b43_pio_tx(struct b43_wldev *dev, struct sk_buff *skb)
495 struct b43_pio_txqueue *q;
496 struct ieee80211_hdr *hdr;
498 unsigned int hdrlen, total_len;
500 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
502 hdr = (struct ieee80211_hdr *)skb->data;
504 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
505 /* The multicast queue will be sent after the DTIM. */
506 q = dev->pio.tx_queue_mcast;
507 /* Set the frame More-Data bit. Ucode will clear it
508 * for us on the last frame. */
509 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
511 /* Decide by priority where to put this frame. */
512 q = select_queue_by_priority(dev, skb_get_queue_mapping(skb));
515 spin_lock_irqsave(&q->lock, flags);
517 hdrlen = b43_txhdr_size(dev);
518 total_len = roundup(skb->len + hdrlen, 4);
520 if (unlikely(total_len > q->buffer_size)) {
522 b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
525 if (unlikely(q->free_packet_slots == 0)) {
527 b43warn(dev->wl, "PIO: TX packet overflow.\n");
530 B43_WARN_ON(q->buffer_used > q->buffer_size);
532 if (total_len > (q->buffer_size - q->buffer_used)) {
533 /* Not enough memory on the queue. */
535 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
540 /* Assign the queue number to the ring (if not already done before)
541 * so TX status handling can use it. The mac80211-queue to b43-queue
542 * mapping is static, so we don't need to store it per frame. */
543 q->queue_prio = skb_get_queue_mapping(skb);
545 err = pio_tx_frame(q, skb);
546 if (unlikely(err == -ENOKEY)) {
547 /* Drop this packet, as we don't have the encryption key
548 * anymore and must not transmit it unencrypted. */
549 dev_kfree_skb_any(skb);
554 b43err(dev->wl, "PIO transmission failure\n");
559 B43_WARN_ON(q->buffer_used > q->buffer_size);
560 if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
561 (q->free_packet_slots == 0)) {
562 /* The queue is full. */
563 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
568 spin_unlock_irqrestore(&q->lock, flags);
573 /* Called with IRQs disabled. */
574 void b43_pio_handle_txstatus(struct b43_wldev *dev,
575 const struct b43_txstatus *status)
577 struct b43_pio_txqueue *q;
578 struct b43_pio_txpacket *pack = NULL;
579 unsigned int total_len;
580 struct ieee80211_tx_info *info;
582 q = parse_cookie(dev, status->cookie, &pack);
587 spin_lock(&q->lock); /* IRQs are already disabled. */
589 info = IEEE80211_SKB_CB(pack->skb);
590 memset(&info->status, 0, sizeof(info->status));
592 b43_fill_txstatus_report(info, status);
594 total_len = pack->skb->len + b43_txhdr_size(dev);
595 total_len = roundup(total_len, 4);
596 q->buffer_used -= total_len;
597 q->free_packet_slots += 1;
599 ieee80211_tx_status_irqsafe(dev->wl->hw, pack->skb);
601 list_add(&pack->list, &q->packets_list);
604 ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
608 spin_unlock(&q->lock);
611 void b43_pio_get_tx_stats(struct b43_wldev *dev,
612 struct ieee80211_tx_queue_stats *stats)
614 const int nr_queues = dev->wl->hw->queues;
615 struct b43_pio_txqueue *q;
619 for (i = 0; i < nr_queues; i++) {
620 q = select_queue_by_priority(dev, i);
622 spin_lock_irqsave(&q->lock, flags);
623 stats[i].len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
624 stats[i].limit = B43_PIO_MAX_NR_TXPACKETS;
625 stats[i].count = q->nr_tx_packets;
626 spin_unlock_irqrestore(&q->lock, flags);
630 /* Returns whether we should fetch another frame. */
631 static bool pio_rx_frame(struct b43_pio_rxqueue *q)
633 struct b43_wldev *dev = q->dev;
634 struct b43_rxhdr_fw4 rxhdr;
637 unsigned int i, padding;
639 const char *err_msg = NULL;
641 memset(&rxhdr, 0, sizeof(rxhdr));
643 /* Check if we have data and wait for it to get ready. */
647 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
648 if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
650 b43_piorx_write32(q, B43_PIO8_RXCTL,
651 B43_PIO8_RXCTL_FRAMERDY);
652 for (i = 0; i < 10; i++) {
653 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
654 if (ctl & B43_PIO8_RXCTL_DATARDY)
661 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
662 if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
664 b43_piorx_write16(q, B43_PIO_RXCTL,
665 B43_PIO_RXCTL_FRAMERDY);
666 for (i = 0; i < 10; i++) {
667 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
668 if (ctl & B43_PIO_RXCTL_DATARDY)
673 b43dbg(q->dev->wl, "PIO RX timed out\n");
677 /* Get the preamble (RX header) */
679 ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
680 q->mmio_base + B43_PIO8_RXDATA,
683 ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
684 q->mmio_base + B43_PIO_RXDATA,
688 len = le16_to_cpu(rxhdr.frame_len);
689 if (unlikely(len > 0x700)) {
690 err_msg = "len > 0x700";
693 if (unlikely(len == 0)) {
694 err_msg = "len == 0";
698 macstat = le32_to_cpu(rxhdr.mac_status);
699 if (macstat & B43_RX_MAC_FCSERR) {
700 if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
701 /* Drop frames with failed FCS. */
702 err_msg = "Frame FCS error";
707 /* We always pad 2 bytes, as that's what upstream code expects
708 * due to the RX-header being 30 bytes. In case the frame is
709 * unaligned, we pad another 2 bytes. */
710 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
711 skb = dev_alloc_skb(len + padding + 2);
712 if (unlikely(!skb)) {
713 err_msg = "Out of memory";
717 skb_put(skb, len + padding);
719 ssb_block_read(dev->dev, skb->data + padding, (len & ~3),
720 q->mmio_base + B43_PIO8_RXDATA,
726 /* Read the last few bytes. */
727 value = b43_piorx_read32(q, B43_PIO8_RXDATA);
728 data = &(skb->data[len + padding - 1]);
731 *data = (value >> 16);
734 *data = (value >> 8);
741 ssb_block_read(dev->dev, skb->data + padding, (len & ~1),
742 q->mmio_base + B43_PIO_RXDATA,
747 /* Read the last byte. */
748 value = b43_piorx_read16(q, B43_PIO_RXDATA);
749 skb->data[len + padding - 1] = value;
753 b43_rx(q->dev, skb, &rxhdr);
759 b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
760 b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
764 /* RX workqueue. We can sleep, yay! */
765 static void b43_pio_rx_work(struct work_struct *work)
767 struct b43_pio_rxqueue *q = container_of(work, struct b43_pio_rxqueue,
769 unsigned int budget = 50;
773 spin_lock_irq(&q->lock);
774 stop = (pio_rx_frame(q) == 0);
775 spin_unlock_irq(&q->lock);
782 /* Called with IRQs disabled. */
783 void b43_pio_rx(struct b43_pio_rxqueue *q)
785 /* Due to latency issues we must run the RX path in
786 * a workqueue to be able to schedule between packets. */
787 queue_work(q->dev->wl->hw->workqueue, &q->rx_work);
790 static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
794 spin_lock_irqsave(&q->lock, flags);
796 b43_piotx_write32(q, B43_PIO8_TXCTL,
797 b43_piotx_read32(q, B43_PIO8_TXCTL)
798 | B43_PIO8_TXCTL_SUSPREQ);
800 b43_piotx_write16(q, B43_PIO_TXCTL,
801 b43_piotx_read16(q, B43_PIO_TXCTL)
802 | B43_PIO_TXCTL_SUSPREQ);
804 spin_unlock_irqrestore(&q->lock, flags);
807 static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
811 spin_lock_irqsave(&q->lock, flags);
813 b43_piotx_write32(q, B43_PIO8_TXCTL,
814 b43_piotx_read32(q, B43_PIO8_TXCTL)
815 & ~B43_PIO8_TXCTL_SUSPREQ);
817 b43_piotx_write16(q, B43_PIO_TXCTL,
818 b43_piotx_read16(q, B43_PIO_TXCTL)
819 & ~B43_PIO_TXCTL_SUSPREQ);
821 spin_unlock_irqrestore(&q->lock, flags);
824 void b43_pio_tx_suspend(struct b43_wldev *dev)
826 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
827 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
828 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
829 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
830 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
831 b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
834 void b43_pio_tx_resume(struct b43_wldev *dev)
836 b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
837 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
838 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
839 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
840 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
841 b43_power_saving_ctl_bits(dev, 0);