hostap_cs: don't match revisions in presense of the MAC chip name
[pandora-kernel.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(*((__le16 *)(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         u16 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 = packet->index;
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                 packet->index = i;
290
291                 list_add(&packet->list, &queue->txfree);
292         }
293 }
294
295 static
296 struct b43_pioqueue *b43_setup_pioqueue(struct b43_wldev *dev,
297                                         u16 pio_mmio_base)
298 {
299         struct b43_pioqueue *queue;
300         u16 qsize;
301
302         queue = kzalloc(sizeof(*queue), GFP_KERNEL);
303         if (!queue)
304                 goto out;
305
306         queue->dev = dev;
307         queue->mmio_base = pio_mmio_base;
308         queue->need_workarounds = (dev->dev->id.revision < 3);
309
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);
314
315         b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
316                     & ~B43_MACCTL_BE);
317
318         qsize = b43_read16(dev, queue->mmio_base + B43_PIO_TXQBUFSIZE);
319         if (qsize == 0) {
320                 b43err(dev->wl, "This card does not support PIO "
321                        "operation mode. Please use DMA mode "
322                        "(module parameter pio=0).\n");
323                 goto err_freequeue;
324         }
325         if (qsize <= B43_PIO_TXQADJUST) {
326                 b43err(dev->wl, "PIO tx device-queue too small (%u)\n", qsize);
327                 goto err_freequeue;
328         }
329         qsize -= B43_PIO_TXQADJUST;
330         queue->tx_devq_size = qsize;
331
332         setup_txqueues(queue);
333
334       out:
335         return queue;
336
337       err_freequeue:
338         kfree(queue);
339         queue = NULL;
340         goto out;
341 }
342
343 static void cancel_transfers(struct b43_pioqueue *queue)
344 {
345         struct b43_pio_txpacket *packet, *tmp_packet;
346
347         tasklet_disable(&queue->txtask);
348
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);
353 }
354
355 static void b43_destroy_pioqueue(struct b43_pioqueue *queue)
356 {
357         if (!queue)
358                 return;
359
360         cancel_transfers(queue);
361         kfree(queue);
362 }
363
364 void b43_pio_free(struct b43_wldev *dev)
365 {
366         struct b43_pio *pio;
367
368         if (!b43_using_pio(dev))
369                 return;
370         pio = &dev->pio;
371
372         b43_destroy_pioqueue(pio->queue3);
373         pio->queue3 = NULL;
374         b43_destroy_pioqueue(pio->queue2);
375         pio->queue2 = NULL;
376         b43_destroy_pioqueue(pio->queue1);
377         pio->queue1 = NULL;
378         b43_destroy_pioqueue(pio->queue0);
379         pio->queue0 = NULL;
380 }
381
382 int b43_pio_init(struct b43_wldev *dev)
383 {
384         struct b43_pio *pio = &dev->pio;
385         struct b43_pioqueue *queue;
386         int err = -ENOMEM;
387
388         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO1_BASE);
389         if (!queue)
390                 goto out;
391         pio->queue0 = queue;
392
393         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO2_BASE);
394         if (!queue)
395                 goto err_destroy0;
396         pio->queue1 = queue;
397
398         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO3_BASE);
399         if (!queue)
400                 goto err_destroy1;
401         pio->queue2 = queue;
402
403         queue = b43_setup_pioqueue(dev, B43_MMIO_PIO4_BASE);
404         if (!queue)
405                 goto err_destroy2;
406         pio->queue3 = queue;
407
408         if (dev->dev->id.revision < 3)
409                 dev->irq_savedstate |= B43_IRQ_PIO_WORKAROUND;
410
411         b43dbg(dev->wl, "PIO initialized\n");
412         err = 0;
413       out:
414         return err;
415
416       err_destroy2:
417         b43_destroy_pioqueue(pio->queue2);
418         pio->queue2 = NULL;
419       err_destroy1:
420         b43_destroy_pioqueue(pio->queue1);
421         pio->queue1 = NULL;
422       err_destroy0:
423         b43_destroy_pioqueue(pio->queue0);
424         pio->queue0 = NULL;
425         goto out;
426 }
427
428 int b43_pio_tx(struct b43_wldev *dev,
429                struct sk_buff *skb, struct ieee80211_tx_control *ctl)
430 {
431         struct b43_pioqueue *queue = dev->pio.queue1;
432         struct b43_pio_txpacket *packet;
433
434         B43_WARN_ON(queue->tx_suspended);
435         B43_WARN_ON(list_empty(&queue->txfree));
436
437         packet = list_entry(queue->txfree.next, struct b43_pio_txpacket, list);
438         packet->skb = skb;
439
440         memset(&packet->txstat, 0, sizeof(packet->txstat));
441         memcpy(&packet->txstat.control, ctl, sizeof(*ctl));
442
443         list_move_tail(&packet->list, &queue->txqueue);
444         queue->nr_txfree--;
445         queue->nr_tx_packets++;
446         B43_WARN_ON(queue->nr_txfree >= B43_PIO_MAXTXPACKETS);
447
448         tasklet_schedule(&queue->txtask);
449
450         return 0;
451 }
452
453 void b43_pio_handle_txstatus(struct b43_wldev *dev,
454                              const struct b43_txstatus *status)
455 {
456         struct b43_pioqueue *queue;
457         struct b43_pio_txpacket *packet;
458
459         queue = parse_cookie(dev, status->cookie, &packet);
460         if (B43_WARN_ON(!queue))
461                 return;
462
463         queue->tx_devq_packets--;
464         queue->tx_devq_used -=
465             (packet->skb->len + sizeof(struct b43_txhdr_fw4));
466
467         if (status->acked) {
468                 packet->txstat.flags |= IEEE80211_TX_STATUS_ACK;
469         } else {
470                 if (!(packet->txstat.control.flags & IEEE80211_TXCTL_NO_ACK))
471                         packet->txstat.excessive_retries = 1;
472         }
473         if (status->frame_count == 0) {
474                 /* The frame was not transmitted at all. */
475                 packet->txstat.retry_count = 0;
476         } else
477                 packet->txstat.retry_count = status->frame_count - 1;
478         ieee80211_tx_status_irqsafe(dev->wl->hw, packet->skb,
479                                     &(packet->txstat));
480         packet->skb = NULL;
481
482         free_txpacket(packet);
483         /* If there are packets on the txqueue, poke the tasklet
484          * to transmit them.
485          */
486         if (!list_empty(&queue->txqueue))
487                 tasklet_schedule(&queue->txtask);
488 }
489
490 void b43_pio_get_tx_stats(struct b43_wldev *dev,
491                           struct ieee80211_tx_queue_stats *stats)
492 {
493         struct b43_pio *pio = &dev->pio;
494         struct b43_pioqueue *queue;
495         struct ieee80211_tx_queue_stats_data *data;
496
497         queue = pio->queue1;
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;
502 }
503
504 static void pio_rx_error(struct b43_pioqueue *queue,
505                          int clear_buffers, const char *error)
506 {
507         int i;
508
509         b43err(queue->dev->wl, "PIO RX error: %s\n", error);
510         b43_pio_write(queue, B43_PIO_RXCTL, B43_PIO_RXCTL_READY);
511         if (clear_buffers) {
512                 B43_WARN_ON(queue->mmio_base != B43_MMIO_PIO1_BASE);
513                 for (i = 0; i < 15; i++) {
514                         /* Dummy read. */
515                         b43_pio_read(queue, B43_PIO_RXDATA);
516                 }
517         }
518 }
519
520 void b43_pio_rx(struct b43_pioqueue *queue)
521 {
522         __le16 preamble[21] = { 0 };
523         struct b43_rxhdr_fw4 *rxhdr;
524         u16 tmp, len;
525         u32 macstat;
526         int i, preamble_readwords;
527         struct sk_buff *skb;
528
529         tmp = b43_pio_read(queue, B43_PIO_RXCTL);
530         if (!(tmp & B43_PIO_RXCTL_DATAAVAILABLE))
531                 return;
532         b43_pio_write(queue, B43_PIO_RXCTL, B43_PIO_RXCTL_DATAAVAILABLE);
533
534         for (i = 0; i < 10; i++) {
535                 tmp = b43_pio_read(queue, B43_PIO_RXCTL);
536                 if (tmp & B43_PIO_RXCTL_READY)
537                         goto data_ready;
538                 udelay(10);
539         }
540         b43dbg(queue->dev->wl, "PIO RX timed out\n");
541         return;
542 data_ready:
543
544         len = b43_pio_read(queue, B43_PIO_RXDATA);
545         if (unlikely(len > 0x700)) {
546                 pio_rx_error(queue, 0, "len > 0x700");
547                 return;
548         }
549         if (unlikely(len == 0 && queue->mmio_base != B43_MMIO_PIO4_BASE)) {
550                 pio_rx_error(queue, 0, "len == 0");
551                 return;
552         }
553         preamble[0] = cpu_to_le16(len);
554         if (queue->mmio_base == B43_MMIO_PIO4_BASE)
555                 preamble_readwords = 14 / sizeof(u16);
556         else
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);
561         }
562         rxhdr = (struct b43_rxhdr_fw4 *)preamble;
563         macstat = le32_to_cpu(rxhdr->mac_status);
564         if (macstat & B43_RX_MAC_FCSERR) {
565                 pio_rx_error(queue,
566                              (queue->mmio_base == B43_MMIO_PIO1_BASE),
567                              "Frame FCS error");
568                 return;
569         }
570         if (queue->mmio_base == B43_MMIO_PIO4_BASE) {
571                 /* We received an xmit status. */
572                 struct b43_hwtxstatus *hw;
573
574                 hw = (struct b43_hwtxstatus *)(preamble + 1);
575                 b43_handle_hwtxstatus(queue->dev, hw);
576
577                 return;
578         }
579
580         skb = dev_alloc_skb(len);
581         if (unlikely(!skb)) {
582                 pio_rx_error(queue, 1, "OOM");
583                 return;
584         }
585         skb_put(skb, len);
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);
589         }
590         if (len % 2) {
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.
596  */
597 #if 0
598                 if (rxflags2 & B43_RXHDR_FLAGS2_TYPE2FRAME)
599                         skb->data[2] = (tmp & 0xFF00) >> 8;
600                 else
601                         skb->data[0] = (tmp & 0xFF00) >> 8;
602 #endif
603         }
604         b43_rx(queue->dev, skb, rxhdr);
605 }
606
607 void b43_pio_tx_suspend(struct b43_pioqueue *queue)
608 {
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);
612 }
613
614 void b43_pio_tx_resume(struct b43_pioqueue *queue)
615 {
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);
620 }
621
622 void b43_pio_freeze_txqueues(struct b43_wldev *dev)
623 {
624         struct b43_pio *pio;
625
626         B43_WARN_ON(!b43_using_pio(dev));
627         pio = &dev->pio;
628         pio->queue0->tx_frozen = 1;
629         pio->queue1->tx_frozen = 1;
630         pio->queue2->tx_frozen = 1;
631         pio->queue3->tx_frozen = 1;
632 }
633
634 void b43_pio_thaw_txqueues(struct b43_wldev *dev)
635 {
636         struct b43_pio *pio;
637
638         B43_WARN_ON(!b43_using_pio(dev));
639         pio = &dev->pio;
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);
652 }