]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/b43/pio.c
[NET]: Introduce and use print_mac() and DECLARE_MAC_BUF()
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / b43 / pio.c
1 /*
2
3   Broadcom B43 wireless driver
4
5   PIO Transmission
6
7   Copyright (c) 2005 Michael Buesch <mb@bu3sch.de>
8
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.
13
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.
18
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.
23
24 */
25
26 #include "b43.h"
27 #include "pio.h"
28 #include "main.h"
29 #include "xmit.h"
30
31 #include <linux/delay.h>
32
33 static void tx_start(struct b43_pioqueue *queue)
34 {
35         b43_pio_write(queue, B43_PIO_TXCTL, B43_PIO_TXCTL_INIT);
36 }
37
38 static void tx_octet(struct b43_pioqueue *queue, u8 octet)
39 {
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);
43         } else {
44                 b43_pio_write(queue, B43_PIO_TXCTL, B43_PIO_TXCTL_WRITELO);
45                 b43_pio_write(queue, B43_PIO_TXDATA, octet);
46         }
47 }
48
49 static u16 tx_get_next_word(const u8 * txhdr,
50                             const u8 * packet,
51                             size_t txhdr_size, unsigned int *pos)
52 {
53         const u8 *source;
54         unsigned int i = *pos;
55         u16 ret;
56
57         if (i < txhdr_size) {
58                 source = txhdr;
59         } else {
60                 source = packet;
61                 i -= txhdr_size;
62         }
63         ret = le16_to_cpu(*((u16 *) (source + i)));
64         *pos += 2;
65
66         return ret;
67 }
68
69 static void tx_data(struct b43_pioqueue *queue,
70                     u8 * txhdr, const u8 * packet, unsigned int octets)
71 {
72         u16 data;
73         unsigned int i = 0;
74
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);
79         }
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);
86         }
87         if (octets % 2)
88                 tx_octet(queue,
89                          packet[octets - sizeof(struct b43_txhdr_fw4) - 1]);
90 }
91
92 static void tx_complete(struct b43_pioqueue *queue, struct sk_buff *skb)
93 {
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);
98         } else {
99                 b43_pio_write(queue, B43_PIO_TXCTL, B43_PIO_TXCTL_COMPLETE);
100         }
101 }
102
103 static u16 generate_cookie(struct b43_pioqueue *queue,
104                            struct b43_pio_txpacket *packet)
105 {
106         u16 cookie = 0x0000;
107         int packetindex;
108
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).
112          */
113         switch (queue->mmio_base) {
114         case B43_MMIO_PIO1_BASE:
115                 break;
116         case B43_MMIO_PIO2_BASE:
117                 cookie = 0x1000;
118                 break;
119         case B43_MMIO_PIO3_BASE:
120                 cookie = 0x2000;
121                 break;
122         case B43_MMIO_PIO4_BASE:
123                 cookie = 0x3000;
124                 break;
125         default:
126                 B43_WARN_ON(1);
127         }
128         packetindex = pio_txpacket_getindex(packet);
129         B43_WARN_ON(packetindex & ~0x0FFF);
130         cookie |= (u16) packetindex;
131
132         return cookie;
133 }
134
135 static
136 struct b43_pioqueue *parse_cookie(struct b43_wldev *dev,
137                                   u16 cookie, struct b43_pio_txpacket **packet)
138 {
139         struct b43_pio *pio = &dev->pio;
140         struct b43_pioqueue *queue = NULL;
141         int packetindex;
142
143         switch (cookie & 0xF000) {
144         case 0x0000:
145                 queue = pio->queue0;
146                 break;
147         case 0x1000:
148                 queue = pio->queue1;
149                 break;
150         case 0x2000:
151                 queue = pio->queue2;
152                 break;
153         case 0x3000:
154                 queue = pio->queue3;
155                 break;
156         default:
157                 B43_WARN_ON(1);
158         }
159         packetindex = (cookie & 0x0FFF);
160         B43_WARN_ON(!(packetindex >= 0 && packetindex < B43_PIO_MAXTXPACKETS));
161         *packet = &(queue->tx_packets_cache[packetindex]);
162
163         return queue;
164 }
165
166 union txhdr_union {
167         struct b43_txhdr_fw4 txhdr_fw4;
168 };
169
170 static void pio_tx_write_fragment(struct b43_pioqueue *queue,
171                                   struct sk_buff *skb,
172                                   struct b43_pio_txpacket *packet,
173                                   size_t txhdr_size)
174 {
175         union txhdr_union txhdr_data;
176         u8 *txhdr = NULL;
177         unsigned int octets;
178
179         txhdr = (u8 *) (&txhdr_data.txhdr_fw4);
180
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));
186
187         tx_start(queue);
188         octets = skb->len + txhdr_size;
189         if (queue->need_workarounds)
190                 octets--;
191         tx_data(queue, txhdr, (u8 *) skb->data, octets);
192         tx_complete(queue, skb);
193 }
194
195 static void free_txpacket(struct b43_pio_txpacket *packet)
196 {
197         struct b43_pioqueue *queue = packet->queue;
198
199         if (packet->skb)
200                 dev_kfree_skb_any(packet->skb);
201         list_move(&packet->list, &queue->txfree);
202         queue->nr_txfree++;
203 }
204
205 static int pio_tx_packet(struct b43_pio_txpacket *packet)
206 {
207         struct b43_pioqueue *queue = packet->queue;
208         struct sk_buff *skb = packet->skb;
209         u16 octets;
210
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);
217                 return 0;
218         }
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
223          * retry later.
224          */
225         if (queue->tx_devq_packets == B43_PIO_MAXTXDEVQPACKETS)
226                 return -EBUSY;
227         if (queue->tx_devq_used + octets > queue->tx_devq_size)
228                 return -EBUSY;
229         /* Now poke the device. */
230         pio_tx_write_fragment(queue, skb, packet, sizeof(struct b43_txhdr_fw4));
231
232         /* Account for the packet size.
233          * (We must not overflow the device TX queue)
234          */
235         queue->tx_devq_packets++;
236         queue->tx_devq_used += octets;
237
238         /* Transmission started, everything ok, move the
239          * packet to the txrunning list.
240          */
241         list_move_tail(&packet->list, &queue->txrunning);
242
243         return 0;
244 }
245
246 static void tx_tasklet(unsigned long d)
247 {
248         struct b43_pioqueue *queue = (struct b43_pioqueue *)d;
249         struct b43_wldev *dev = queue->dev;
250         unsigned long flags;
251         struct b43_pio_txpacket *packet, *tmp_packet;
252         int err;
253         u16 txctl;
254
255         spin_lock_irqsave(&dev->wl->irq_lock, flags);
256         if (queue->tx_frozen)
257                 goto out_unlock;
258         txctl = b43_pio_read(queue, B43_PIO_TXCTL);
259         if (txctl & B43_PIO_TXCTL_SUSPEND)
260                 goto out_unlock;
261
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
268                  * is dropped.
269                  */
270                 err = pio_tx_packet(packet);
271                 if (err)
272                         break;
273         }
274       out_unlock:
275         spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
276 }
277
278 static void setup_txqueues(struct b43_pioqueue *queue)
279 {
280         struct b43_pio_txpacket *packet;
281         int i;
282
283         queue->nr_txfree = B43_PIO_MAXTXPACKETS;
284         for (i = 0; i < B43_PIO_MAXTXPACKETS; i++) {
285                 packet = &(queue->tx_packets_cache[i]);
286
287                 packet->queue = queue;
288                 INIT_LIST_HEAD(&packet->list);
289
290                 list_add(&packet->list, &queue->txfree);
291         }
292 }
293
294 static
295 struct b43_pioqueue *b43_setup_pioqueue(struct b43_wldev *dev,
296                                         u16 pio_mmio_base)
297 {
298         struct b43_pioqueue *queue;
299         u16 qsize;
300
301         queue = kzalloc(sizeof(*queue), GFP_KERNEL);
302         if (!queue)
303                 goto out;
304
305         queue->dev = dev;
306         queue->mmio_base = pio_mmio_base;
307         queue->need_workarounds = (dev->dev->id.revision < 3);
308
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);
313
314         b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
315                     & ~B43_MACCTL_BE);
316
317         qsize = b43_read16(dev, queue->mmio_base + B43_PIO_TXQBUFSIZE);
318         if (qsize == 0) {
319                 b43err(dev->wl, "This card does not support PIO "
320                        "operation mode. Please use DMA mode "
321                        "(module parameter pio=0).\n");
322                 goto err_freequeue;
323         }
324         if (qsize <= B43_PIO_TXQADJUST) {
325                 b43err(dev->wl, "PIO tx device-queue too small (%u)\n", qsize);
326                 goto err_freequeue;
327         }
328         qsize -= B43_PIO_TXQADJUST;
329         queue->tx_devq_size = qsize;
330
331         setup_txqueues(queue);
332
333       out:
334         return queue;
335
336       err_freequeue:
337         kfree(queue);
338         queue = NULL;
339         goto out;
340 }
341
342 static void cancel_transfers(struct b43_pioqueue *queue)
343 {
344         struct b43_pio_txpacket *packet, *tmp_packet;
345
346         tasklet_disable(&queue->txtask);
347
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);
352 }
353
354 static void b43_destroy_pioqueue(struct b43_pioqueue *queue)
355 {
356         if (!queue)
357                 return;
358
359         cancel_transfers(queue);
360         kfree(queue);
361 }
362
363 void b43_pio_free(struct b43_wldev *dev)
364 {
365         struct b43_pio *pio;
366
367         if (!b43_using_pio(dev))
368                 return;
369         pio = &dev->pio;
370
371         b43_destroy_pioqueue(pio->queue3);
372         pio->queue3 = NULL;
373         b43_destroy_pioqueue(pio->queue2);
374         pio->queue2 = NULL;
375         b43_destroy_pioqueue(pio->queue1);
376         pio->queue1 = NULL;
377         b43_destroy_pioqueue(pio->queue0);
378         pio->queue0 = NULL;
379 }
380
381 int b43_pio_init(struct b43_wldev *dev)
382 {
383         struct b43_pio *pio = &dev->pio;
384         struct b43_pioqueue *queue;
385         int err = -ENOMEM;
386
387         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO1_BASE);
388         if (!queue)
389                 goto out;
390         pio->queue0 = queue;
391
392         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO2_BASE);
393         if (!queue)
394                 goto err_destroy0;
395         pio->queue1 = queue;
396
397         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO3_BASE);
398         if (!queue)
399                 goto err_destroy1;
400         pio->queue2 = queue;
401
402         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO4_BASE);
403         if (!queue)
404                 goto err_destroy2;
405         pio->queue3 = queue;
406
407         if (dev->dev->id.revision < 3)
408                 dev->irq_savedstate |= B43_IRQ_PIO_WORKAROUND;
409
410         b43dbg(dev->wl, "PIO initialized\n");
411         err = 0;
412       out:
413         return err;
414
415       err_destroy2:
416         b43_destroy_pioqueue(pio->queue2);
417         pio->queue2 = NULL;
418       err_destroy1:
419         b43_destroy_pioqueue(pio->queue1);
420         pio->queue1 = NULL;
421       err_destroy0:
422         b43_destroy_pioqueue(pio->queue0);
423         pio->queue0 = NULL;
424         goto out;
425 }
426
427 int b43_pio_tx(struct b43_wldev *dev,
428                struct sk_buff *skb, struct ieee80211_tx_control *ctl)
429 {
430         struct b43_pioqueue *queue = dev->pio.queue1;
431         struct b43_pio_txpacket *packet;
432
433         B43_WARN_ON(queue->tx_suspended);
434         B43_WARN_ON(list_empty(&queue->txfree));
435
436         packet = list_entry(queue->txfree.next, struct b43_pio_txpacket, list);
437         packet->skb = skb;
438
439         memset(&packet->txstat, 0, sizeof(packet->txstat));
440         memcpy(&packet->txstat.control, ctl, sizeof(*ctl));
441
442         list_move_tail(&packet->list, &queue->txqueue);
443         queue->nr_txfree--;
444         queue->nr_tx_packets++;
445         B43_WARN_ON(queue->nr_txfree >= B43_PIO_MAXTXPACKETS);
446
447         tasklet_schedule(&queue->txtask);
448
449         return 0;
450 }
451
452 void b43_pio_handle_txstatus(struct b43_wldev *dev,
453                              const struct b43_txstatus *status)
454 {
455         struct b43_pioqueue *queue;
456         struct b43_pio_txpacket *packet;
457
458         queue = parse_cookie(dev, status->cookie, &packet);
459         if (B43_WARN_ON(!queue))
460                 return;
461
462         queue->tx_devq_packets--;
463         queue->tx_devq_used -=
464             (packet->skb->len + sizeof(struct b43_txhdr_fw4));
465
466         if (status->acked) {
467                 packet->txstat.flags |= IEEE80211_TX_STATUS_ACK;
468         } else {
469                 if (!(packet->txstat.control.flags & IEEE80211_TXCTL_NO_ACK))
470                         packet->txstat.excessive_retries = 1;
471         }
472         if (status->frame_count == 0) {
473                 /* The frame was not transmitted at all. */
474                 packet->txstat.retry_count = 0;
475         } else
476                 packet->txstat.retry_count = status->frame_count - 1;
477         ieee80211_tx_status_irqsafe(dev->wl->hw, packet->skb,
478                                     &(packet->txstat));
479         packet->skb = NULL;
480
481         free_txpacket(packet);
482         /* If there are packets on the txqueue, poke the tasklet
483          * to transmit them.
484          */
485         if (!list_empty(&queue->txqueue))
486                 tasklet_schedule(&queue->txtask);
487 }
488
489 void b43_pio_get_tx_stats(struct b43_wldev *dev,
490                           struct ieee80211_tx_queue_stats *stats)
491 {
492         struct b43_pio *pio = &dev->pio;
493         struct b43_pioqueue *queue;
494         struct ieee80211_tx_queue_stats_data *data;
495
496         queue = pio->queue1;
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;
501 }
502
503 static void pio_rx_error(struct b43_pioqueue *queue,
504                          int clear_buffers, const char *error)
505 {
506         int i;
507
508         b43err(queue->dev->wl, "PIO RX error: %s\n", error);
509         b43_pio_write(queue, B43_PIO_RXCTL, B43_PIO_RXCTL_READY);
510         if (clear_buffers) {
511                 B43_WARN_ON(queue->mmio_base != B43_MMIO_PIO1_BASE);
512                 for (i = 0; i < 15; i++) {
513                         /* Dummy read. */
514                         b43_pio_read(queue, B43_PIO_RXDATA);
515                 }
516         }
517 }
518
519 void b43_pio_rx(struct b43_pioqueue *queue)
520 {
521         u16 preamble[21] = { 0 };
522         struct b43_rxhdr_fw4 *rxhdr;
523         u16 tmp, len, macstat;
524         int i, preamble_readwords;
525         struct sk_buff *skb;
526
527         tmp = b43_pio_read(queue, B43_PIO_RXCTL);
528         if (!(tmp & B43_PIO_RXCTL_DATAAVAILABLE))
529                 return;
530         b43_pio_write(queue, B43_PIO_RXCTL, B43_PIO_RXCTL_DATAAVAILABLE);
531
532         for (i = 0; i < 10; i++) {
533                 tmp = b43_pio_read(queue, B43_PIO_RXCTL);
534                 if (tmp & B43_PIO_RXCTL_READY)
535                         goto data_ready;
536                 udelay(10);
537         }
538         b43dbg(queue->dev->wl, "PIO RX timed out\n");
539         return;
540       data_ready:
541
542         len = b43_pio_read(queue, B43_PIO_RXDATA);
543         if (unlikely(len > 0x700)) {
544                 pio_rx_error(queue, 0, "len > 0x700");
545                 return;
546         }
547         if (unlikely(len == 0 && queue->mmio_base != B43_MMIO_PIO4_BASE)) {
548                 pio_rx_error(queue, 0, "len == 0");
549                 return;
550         }
551         preamble[0] = cpu_to_le16(len);
552         if (queue->mmio_base == B43_MMIO_PIO4_BASE)
553                 preamble_readwords = 14 / sizeof(u16);
554         else
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);
559         }
560         rxhdr = (struct b43_rxhdr_fw4 *)preamble;
561         macstat = le16_to_cpu(rxhdr->mac_status);
562         if (macstat & B43_RX_MAC_FCSERR) {
563                 pio_rx_error(queue,
564                              (queue->mmio_base == B43_MMIO_PIO1_BASE),
565                              "Frame FCS error");
566                 return;
567         }
568         if (queue->mmio_base == B43_MMIO_PIO4_BASE) {
569                 /* We received an xmit status. */
570                 struct b43_hwtxstatus *hw;
571
572                 hw = (struct b43_hwtxstatus *)(preamble + 1);
573                 b43_handle_hwtxstatus(queue->dev, hw);
574
575                 return;
576         }
577
578         skb = dev_alloc_skb(len);
579         if (unlikely(!skb)) {
580                 pio_rx_error(queue, 1, "OOM");
581                 return;
582         }
583         skb_put(skb, len);
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);
587         }
588         if (len % 2) {
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.
594  */
595 #if 0
596                 if (rxflags2 & B43_RXHDR_FLAGS2_TYPE2FRAME)
597                         skb->data[2] = (tmp & 0xFF00) >> 8;
598                 else
599                         skb->data[0] = (tmp & 0xFF00) >> 8;
600 #endif
601         }
602         b43_rx(queue->dev, skb, rxhdr);
603 }
604
605 void b43_pio_tx_suspend(struct b43_pioqueue *queue)
606 {
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);
610 }
611
612 void b43_pio_tx_resume(struct b43_pioqueue *queue)
613 {
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);
618 }
619
620 void b43_pio_freeze_txqueues(struct b43_wldev *dev)
621 {
622         struct b43_pio *pio;
623
624         B43_WARN_ON(!b43_using_pio(dev));
625         pio = &dev->pio;
626         pio->queue0->tx_frozen = 1;
627         pio->queue1->tx_frozen = 1;
628         pio->queue2->tx_frozen = 1;
629         pio->queue3->tx_frozen = 1;
630 }
631
632 void b43_pio_thaw_txqueues(struct b43_wldev *dev)
633 {
634         struct b43_pio *pio;
635
636         B43_WARN_ON(!b43_using_pio(dev));
637         pio = &dev->pio;
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);
650 }