blob: c59ddd40680d339122ae6fa48d01c9c754ab8488 [file] [log] [blame]
John W. Linvillef2223132006-01-23 16:59:58 -05001/*
2
3 Broadcom BCM43xx wireless driver
4
5 PIO Transmission
6
7 Copyright (c) 2005 Michael Buesch <mbuesch@freenet.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 "bcm43xx.h"
27#include "bcm43xx_pio.h"
28#include "bcm43xx_main.h"
Michael Bueschf398f022006-02-23 21:15:39 +010029#include "bcm43xx_xmit.h"
John W. Linvillef2223132006-01-23 16:59:58 -050030
31#include <linux/delay.h>
32
33
Michael Buesch77db31e2006-02-12 16:47:44 +010034static void tx_start(struct bcm43xx_pioqueue *queue)
John W. Linvillef2223132006-01-23 16:59:58 -050035{
Michael Buesch77db31e2006-02-12 16:47:44 +010036 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
37 BCM43xx_PIO_TXCTL_INIT);
John W. Linvillef2223132006-01-23 16:59:58 -050038}
39
Michael Buesch77db31e2006-02-12 16:47:44 +010040static void tx_octet(struct bcm43xx_pioqueue *queue,
41 u8 octet)
John W. Linvillef2223132006-01-23 16:59:58 -050042{
Michael Buesch77db31e2006-02-12 16:47:44 +010043 if (queue->need_workarounds) {
44 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
45 octet);
46 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
47 BCM43xx_PIO_TXCTL_WRITEHI);
John W. Linvillef2223132006-01-23 16:59:58 -050048 } else {
Michael Buesch77db31e2006-02-12 16:47:44 +010049 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
50 BCM43xx_PIO_TXCTL_WRITEHI);
51 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
52 octet);
John W. Linvillef2223132006-01-23 16:59:58 -050053 }
54}
55
Michael Buesch77db31e2006-02-12 16:47:44 +010056static u16 tx_get_next_word(struct bcm43xx_txhdr *txhdr,
57 const u8 *packet,
58 unsigned int *pos)
59{
60 const u8 *source;
61 unsigned int i = *pos;
62 u16 ret;
63
64 if (i < sizeof(*txhdr)) {
65 source = (const u8 *)txhdr;
66 } else {
67 source = packet;
68 i -= sizeof(*txhdr);
69 }
70 ret = le16_to_cpu( *((u16 *)(source + i)) );
71 *pos += 2;
72
73 return ret;
74}
75
76static void tx_data(struct bcm43xx_pioqueue *queue,
77 struct bcm43xx_txhdr *txhdr,
78 const u8 *packet,
79 unsigned int octets)
John W. Linvillef2223132006-01-23 16:59:58 -050080{
81 u16 data;
82 unsigned int i = 0;
83
Michael Buesch77db31e2006-02-12 16:47:44 +010084 if (queue->need_workarounds) {
85 data = tx_get_next_word(txhdr, packet, &i);
John W. Linvillef2223132006-01-23 16:59:58 -050086 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA, data);
John W. Linvillef2223132006-01-23 16:59:58 -050087 }
88 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch77db31e2006-02-12 16:47:44 +010089 BCM43xx_PIO_TXCTL_WRITELO |
90 BCM43xx_PIO_TXCTL_WRITEHI);
91 while (i < octets - 1) {
92 data = tx_get_next_word(txhdr, packet, &i);
John W. Linvillef2223132006-01-23 16:59:58 -050093 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA, data);
94 }
95 if (octets % 2)
Michael Buesch77db31e2006-02-12 16:47:44 +010096 tx_octet(queue, packet[octets - sizeof(*txhdr) - 1]);
John W. Linvillef2223132006-01-23 16:59:58 -050097}
98
Michael Buesch77db31e2006-02-12 16:47:44 +010099static void tx_complete(struct bcm43xx_pioqueue *queue,
100 struct sk_buff *skb)
John W. Linvillef2223132006-01-23 16:59:58 -0500101{
Michael Buesch77db31e2006-02-12 16:47:44 +0100102 if (queue->need_workarounds) {
103 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
104 skb->data[skb->len - 1]);
John W. Linvillef2223132006-01-23 16:59:58 -0500105 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch77db31e2006-02-12 16:47:44 +0100106 BCM43xx_PIO_TXCTL_WRITEHI |
107 BCM43xx_PIO_TXCTL_COMPLETE);
John W. Linvillef2223132006-01-23 16:59:58 -0500108 } else {
Michael Buesch77db31e2006-02-12 16:47:44 +0100109 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
110 BCM43xx_PIO_TXCTL_COMPLETE);
John W. Linvillef2223132006-01-23 16:59:58 -0500111 }
112}
113
Michael Buesch77db31e2006-02-12 16:47:44 +0100114static u16 generate_cookie(struct bcm43xx_pioqueue *queue,
115 int packetindex)
John W. Linvillef2223132006-01-23 16:59:58 -0500116{
117 u16 cookie = 0x0000;
118
119 /* We use the upper 4 bits for the PIO
120 * controller ID and the lower 12 bits
121 * for the packet index (in the cache).
122 */
123 switch (queue->mmio_base) {
John W. Linvillef2223132006-01-23 16:59:58 -0500124 case BCM43xx_MMIO_PIO1_BASE:
125 break;
126 case BCM43xx_MMIO_PIO2_BASE:
127 cookie = 0x1000;
128 break;
129 case BCM43xx_MMIO_PIO3_BASE:
130 cookie = 0x2000;
131 break;
132 case BCM43xx_MMIO_PIO4_BASE:
133 cookie = 0x3000;
134 break;
Michael Buesch77db31e2006-02-12 16:47:44 +0100135 default:
136 assert(0);
John W. Linvillef2223132006-01-23 16:59:58 -0500137 }
138 assert(((u16)packetindex & 0xF000) == 0x0000);
139 cookie |= (u16)packetindex;
140
141 return cookie;
142}
143
Michael Buesch77db31e2006-02-12 16:47:44 +0100144static
John W. Linvillef2223132006-01-23 16:59:58 -0500145struct bcm43xx_pioqueue * parse_cookie(struct bcm43xx_private *bcm,
146 u16 cookie,
147 struct bcm43xx_pio_txpacket **packet)
148{
Michael Buesche9357c02006-03-13 19:27:34 +0100149 struct bcm43xx_pio *pio = bcm43xx_current_pio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500150 struct bcm43xx_pioqueue *queue = NULL;
151 int packetindex;
152
153 switch (cookie & 0xF000) {
154 case 0x0000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100155 queue = pio->queue0;
John W. Linvillef2223132006-01-23 16:59:58 -0500156 break;
157 case 0x1000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100158 queue = pio->queue1;
John W. Linvillef2223132006-01-23 16:59:58 -0500159 break;
160 case 0x2000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100161 queue = pio->queue2;
John W. Linvillef2223132006-01-23 16:59:58 -0500162 break;
163 case 0x3000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100164 queue = pio->queue3;
John W. Linvillef2223132006-01-23 16:59:58 -0500165 break;
166 default:
167 assert(0);
168 }
John W. Linvillef2223132006-01-23 16:59:58 -0500169 packetindex = (cookie & 0x0FFF);
170 assert(packetindex >= 0 && packetindex < BCM43xx_PIO_MAXTXPACKETS);
Michael Buesch77db31e2006-02-12 16:47:44 +0100171 *packet = &(queue->tx_packets_cache[packetindex]);
John W. Linvillef2223132006-01-23 16:59:58 -0500172
173 return queue;
174}
175
Michael Buesch77db31e2006-02-12 16:47:44 +0100176static void pio_tx_write_fragment(struct bcm43xx_pioqueue *queue,
177 struct sk_buff *skb,
178 struct bcm43xx_pio_txpacket *packet)
John W. Linvillef2223132006-01-23 16:59:58 -0500179{
Michael Buesch77db31e2006-02-12 16:47:44 +0100180 struct bcm43xx_txhdr txhdr;
John W. Linvillef2223132006-01-23 16:59:58 -0500181 unsigned int octets;
182
183 assert(skb_shinfo(skb)->nr_frags == 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500184 bcm43xx_generate_txhdr(queue->bcm,
Michael Buesch77db31e2006-02-12 16:47:44 +0100185 &txhdr, skb->data, skb->len,
John W. Linvillef2223132006-01-23 16:59:58 -0500186 (packet->xmitted_frags == 0),
187 generate_cookie(queue, pio_txpacket_getindex(packet)));
188
189 tx_start(queue);
Michael Buesch77db31e2006-02-12 16:47:44 +0100190 octets = skb->len + sizeof(txhdr);
191 if (queue->need_workarounds)
192 octets--;
193 tx_data(queue, &txhdr, (u8 *)skb->data, octets);
John W. Linvillef2223132006-01-23 16:59:58 -0500194 tx_complete(queue, skb);
195}
196
Michael Buesch77db31e2006-02-12 16:47:44 +0100197static void free_txpacket(struct bcm43xx_pio_txpacket *packet,
198 int irq_context)
199{
200 struct bcm43xx_pioqueue *queue = packet->queue;
201
202 ieee80211_txb_free(packet->txb);
203 list_move(&packet->list, &queue->txfree);
204 queue->nr_txfree++;
205
206 assert(queue->tx_devq_used >= packet->xmitted_octets);
207 assert(queue->tx_devq_packets >= packet->xmitted_frags);
208 queue->tx_devq_used -= packet->xmitted_octets;
209 queue->tx_devq_packets -= packet->xmitted_frags;
210}
211
212static int pio_tx_packet(struct bcm43xx_pio_txpacket *packet)
John W. Linvillef2223132006-01-23 16:59:58 -0500213{
214 struct bcm43xx_pioqueue *queue = packet->queue;
215 struct ieee80211_txb *txb = packet->txb;
216 struct sk_buff *skb;
217 u16 octets;
218 int i;
219
220 for (i = packet->xmitted_frags; i < txb->nr_frags; i++) {
221 skb = txb->fragments[i];
222
223 octets = (u16)skb->len + sizeof(struct bcm43xx_txhdr);
John W. Linvillef2223132006-01-23 16:59:58 -0500224 assert(queue->tx_devq_size >= octets);
225 assert(queue->tx_devq_packets <= BCM43xx_PIO_MAXTXDEVQPACKETS);
226 assert(queue->tx_devq_used <= queue->tx_devq_size);
227 /* Check if there is sufficient free space on the device
Michael Buesch77db31e2006-02-12 16:47:44 +0100228 * TX queue. If not, return and let the TX tasklet
John W. Linvillef2223132006-01-23 16:59:58 -0500229 * retry later.
230 */
231 if (queue->tx_devq_packets == BCM43xx_PIO_MAXTXDEVQPACKETS)
232 return -EBUSY;
233 if (queue->tx_devq_used + octets > queue->tx_devq_size)
234 return -EBUSY;
235 /* Now poke the device. */
236 pio_tx_write_fragment(queue, skb, packet);
237
238 /* Account for the packet size.
239 * (We must not overflow the device TX queue)
240 */
241 queue->tx_devq_packets++;
242 queue->tx_devq_used += octets;
243
244 assert(packet->xmitted_frags <= packet->txb->nr_frags);
245 packet->xmitted_frags++;
246 packet->xmitted_octets += octets;
247 }
248 list_move_tail(&packet->list, &queue->txrunning);
249
250 return 0;
251}
252
Michael Buesch77db31e2006-02-12 16:47:44 +0100253static void tx_tasklet(unsigned long d)
John W. Linvillef2223132006-01-23 16:59:58 -0500254{
Michael Buesch77db31e2006-02-12 16:47:44 +0100255 struct bcm43xx_pioqueue *queue = (struct bcm43xx_pioqueue *)d;
256 struct bcm43xx_private *bcm = queue->bcm;
John W. Linvillef2223132006-01-23 16:59:58 -0500257 unsigned long flags;
258 struct bcm43xx_pio_txpacket *packet, *tmp_packet;
259 int err;
260
Michael Bueschefccb642006-03-11 13:39:14 +0100261 bcm43xx_lock_mmio(bcm, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500262 list_for_each_entry_safe(packet, tmp_packet, &queue->txqueue, list) {
263 assert(packet->xmitted_frags < packet->txb->nr_frags);
264 if (packet->xmitted_frags == 0) {
265 int i;
266 struct sk_buff *skb;
267
268 /* Check if the device queue is big
269 * enough for every fragment. If not, drop the
270 * whole packet.
271 */
272 for (i = 0; i < packet->txb->nr_frags; i++) {
273 skb = packet->txb->fragments[i];
274 if (unlikely(skb->len > queue->tx_devq_size)) {
275 dprintkl(KERN_ERR PFX "PIO TX device queue too small. "
Michael Buesch77db31e2006-02-12 16:47:44 +0100276 "Dropping packet.\n");
277 free_txpacket(packet, 1);
John W. Linvillef2223132006-01-23 16:59:58 -0500278 goto next_packet;
279 }
280 }
281 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100282 /* Try to transmit the packet.
John W. Linvillef2223132006-01-23 16:59:58 -0500283 * This may not completely succeed.
284 */
285 err = pio_tx_packet(packet);
286 if (err)
287 break;
Michael Buesch77db31e2006-02-12 16:47:44 +0100288 next_packet:
John W. Linvillef2223132006-01-23 16:59:58 -0500289 continue;
290 }
Michael Bueschefccb642006-03-11 13:39:14 +0100291 bcm43xx_unlock_mmio(bcm, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500292}
293
294static void setup_txqueues(struct bcm43xx_pioqueue *queue)
295{
296 struct bcm43xx_pio_txpacket *packet;
297 int i;
298
Michael Buesch77db31e2006-02-12 16:47:44 +0100299 queue->nr_txfree = BCM43xx_PIO_MAXTXPACKETS;
John W. Linvillef2223132006-01-23 16:59:58 -0500300 for (i = 0; i < BCM43xx_PIO_MAXTXPACKETS; i++) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100301 packet = &(queue->tx_packets_cache[i]);
John W. Linvillef2223132006-01-23 16:59:58 -0500302
303 packet->queue = queue;
304 INIT_LIST_HEAD(&packet->list);
305
306 list_add(&packet->list, &queue->txfree);
307 }
308}
309
310static
311struct bcm43xx_pioqueue * bcm43xx_setup_pioqueue(struct bcm43xx_private *bcm,
312 u16 pio_mmio_base)
313{
314 struct bcm43xx_pioqueue *queue;
315 u32 value;
316 u16 qsize;
317
Michael Buesch77db31e2006-02-12 16:47:44 +0100318 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
John W. Linvillef2223132006-01-23 16:59:58 -0500319 if (!queue)
320 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -0500321
322 queue->bcm = bcm;
323 queue->mmio_base = pio_mmio_base;
Michael Buesch77db31e2006-02-12 16:47:44 +0100324 queue->need_workarounds = (bcm->current_core->rev < 3);
John W. Linvillef2223132006-01-23 16:59:58 -0500325
326 INIT_LIST_HEAD(&queue->txfree);
327 INIT_LIST_HEAD(&queue->txqueue);
328 INIT_LIST_HEAD(&queue->txrunning);
Michael Buesch77db31e2006-02-12 16:47:44 +0100329 tasklet_init(&queue->txtask, tx_tasklet,
330 (unsigned long)queue);
John W. Linvillef2223132006-01-23 16:59:58 -0500331
332 value = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
333 value |= BCM43xx_SBF_XFER_REG_BYTESWAP;
334 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, value);
335
336 qsize = bcm43xx_read16(bcm, queue->mmio_base + BCM43xx_PIO_TXQBUFSIZE);
337 if (qsize <= BCM43xx_PIO_TXQADJUST) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100338 printk(KERN_ERR PFX "PIO tx device-queue too small (%u)\n", qsize);
John W. Linvillef2223132006-01-23 16:59:58 -0500339 goto err_freequeue;
340 }
341 qsize -= BCM43xx_PIO_TXQADJUST;
342 queue->tx_devq_size = qsize;
343
344 setup_txqueues(queue);
345
346out:
347 return queue;
348
349err_freequeue:
350 kfree(queue);
351 queue = NULL;
352 goto out;
353}
354
355static void cancel_transfers(struct bcm43xx_pioqueue *queue)
356{
357 struct bcm43xx_pio_txpacket *packet, *tmp_packet;
358
359 netif_tx_disable(queue->bcm->net_dev);
360 assert(queue->bcm->shutting_down);
Michael Buesch77db31e2006-02-12 16:47:44 +0100361 tasklet_disable(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500362
363 list_for_each_entry_safe(packet, tmp_packet, &queue->txrunning, list)
Michael Buesch77db31e2006-02-12 16:47:44 +0100364 free_txpacket(packet, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500365 list_for_each_entry_safe(packet, tmp_packet, &queue->txqueue, list)
Michael Buesch77db31e2006-02-12 16:47:44 +0100366 free_txpacket(packet, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500367}
368
369static void bcm43xx_destroy_pioqueue(struct bcm43xx_pioqueue *queue)
370{
371 if (!queue)
372 return;
373
374 cancel_transfers(queue);
375 kfree(queue);
376}
377
378void bcm43xx_pio_free(struct bcm43xx_private *bcm)
379{
Michael Buesch49f29efa2006-03-14 16:05:26 +0100380 struct bcm43xx_pio *pio;
381
382 if (!bcm43xx_using_pio(bcm))
383 return;
384 pio = bcm43xx_current_pio(bcm);
Michael Buesch77db31e2006-02-12 16:47:44 +0100385
386 bcm43xx_destroy_pioqueue(pio->queue3);
387 pio->queue3 = NULL;
388 bcm43xx_destroy_pioqueue(pio->queue2);
389 pio->queue2 = NULL;
390 bcm43xx_destroy_pioqueue(pio->queue1);
391 pio->queue1 = NULL;
392 bcm43xx_destroy_pioqueue(pio->queue0);
393 pio->queue0 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500394}
395
396int bcm43xx_pio_init(struct bcm43xx_private *bcm)
397{
Michael Buesche9357c02006-03-13 19:27:34 +0100398 struct bcm43xx_pio *pio = bcm43xx_current_pio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500399 struct bcm43xx_pioqueue *queue;
400 int err = -ENOMEM;
401
402 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO1_BASE);
403 if (!queue)
404 goto out;
Michael Buesch77db31e2006-02-12 16:47:44 +0100405 pio->queue0 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500406
407 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO2_BASE);
408 if (!queue)
409 goto err_destroy0;
Michael Buesch77db31e2006-02-12 16:47:44 +0100410 pio->queue1 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500411
412 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO3_BASE);
413 if (!queue)
414 goto err_destroy1;
Michael Buesch77db31e2006-02-12 16:47:44 +0100415 pio->queue2 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500416
417 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO4_BASE);
418 if (!queue)
419 goto err_destroy2;
Michael Buesch77db31e2006-02-12 16:47:44 +0100420 pio->queue3 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500421
422 if (bcm->current_core->rev < 3)
423 bcm->irq_savedstate |= BCM43xx_IRQ_PIO_WORKAROUND;
424
425 dprintk(KERN_INFO PFX "PIO initialized\n");
426 err = 0;
427out:
428 return err;
429
430err_destroy2:
Michael Buesch77db31e2006-02-12 16:47:44 +0100431 bcm43xx_destroy_pioqueue(pio->queue2);
432 pio->queue2 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500433err_destroy1:
Michael Buesch77db31e2006-02-12 16:47:44 +0100434 bcm43xx_destroy_pioqueue(pio->queue1);
435 pio->queue1 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500436err_destroy0:
Michael Buesch77db31e2006-02-12 16:47:44 +0100437 bcm43xx_destroy_pioqueue(pio->queue0);
438 pio->queue0 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500439 goto out;
440}
441
Michael Buesch77db31e2006-02-12 16:47:44 +0100442int bcm43xx_pio_tx(struct bcm43xx_private *bcm,
443 struct ieee80211_txb *txb)
John W. Linvillef2223132006-01-23 16:59:58 -0500444{
Michael Buesche9357c02006-03-13 19:27:34 +0100445 struct bcm43xx_pioqueue *queue = bcm43xx_current_pio(bcm)->queue1;
John W. Linvillef2223132006-01-23 16:59:58 -0500446 struct bcm43xx_pio_txpacket *packet;
John W. Linvillef2223132006-01-23 16:59:58 -0500447 u16 tmp;
448
John W. Linvillef2223132006-01-23 16:59:58 -0500449 assert(!queue->tx_suspended);
450 assert(!list_empty(&queue->txfree));
451
452 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_TXCTL);
Michael Buesch77db31e2006-02-12 16:47:44 +0100453 if (tmp & BCM43xx_PIO_TXCTL_SUSPEND)
John W. Linvillef2223132006-01-23 16:59:58 -0500454 return -EBUSY;
John W. Linvillef2223132006-01-23 16:59:58 -0500455
456 packet = list_entry(queue->txfree.next, struct bcm43xx_pio_txpacket, list);
John W. Linvillef2223132006-01-23 16:59:58 -0500457 packet->txb = txb;
John W. Linvillef2223132006-01-23 16:59:58 -0500458 packet->xmitted_frags = 0;
Michael Buesch77db31e2006-02-12 16:47:44 +0100459 packet->xmitted_octets = 0;
460 list_move_tail(&packet->list, &queue->txqueue);
461 queue->nr_txfree--;
462 assert(queue->nr_txfree < BCM43xx_PIO_MAXTXPACKETS);
John W. Linvillef2223132006-01-23 16:59:58 -0500463
464 /* Suspend TX, if we are out of packets in the "free" queue. */
465 if (unlikely(list_empty(&queue->txfree))) {
466 netif_stop_queue(queue->bcm->net_dev);
467 queue->tx_suspended = 1;
468 }
469
Michael Buesch77db31e2006-02-12 16:47:44 +0100470 tasklet_schedule(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500471
472 return 0;
473}
474
Michael Buesch77db31e2006-02-12 16:47:44 +0100475void bcm43xx_pio_handle_xmitstatus(struct bcm43xx_private *bcm,
476 struct bcm43xx_xmitstatus *status)
John W. Linvillef2223132006-01-23 16:59:58 -0500477{
478 struct bcm43xx_pioqueue *queue;
479 struct bcm43xx_pio_txpacket *packet;
John W. Linvillef2223132006-01-23 16:59:58 -0500480
481 queue = parse_cookie(bcm, status->cookie, &packet);
482 assert(queue);
Michael Buesch77db31e2006-02-12 16:47:44 +0100483//TODO
484if (!queue)
485return;
486 free_txpacket(packet, 1);
John W. Linvillef2223132006-01-23 16:59:58 -0500487 if (unlikely(queue->tx_suspended)) {
488 queue->tx_suspended = 0;
489 netif_wake_queue(queue->bcm->net_dev);
490 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100491 /* If there are packets on the txqueue, poke the tasklet. */
492 if (!list_empty(&queue->txqueue))
493 tasklet_schedule(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500494}
495
496static void pio_rx_error(struct bcm43xx_pioqueue *queue,
Michael Buesch77db31e2006-02-12 16:47:44 +0100497 int clear_buffers,
John W. Linvillef2223132006-01-23 16:59:58 -0500498 const char *error)
499{
Michael Buesch77db31e2006-02-12 16:47:44 +0100500 int i;
501
502 printkl("PIO RX error: %s\n", error);
503 bcm43xx_pio_write(queue, BCM43xx_PIO_RXCTL,
504 BCM43xx_PIO_RXCTL_READY);
505 if (clear_buffers) {
506 assert(queue->mmio_base == BCM43xx_MMIO_PIO1_BASE);
507 for (i = 0; i < 15; i++) {
508 /* Dummy read. */
509 bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
510 }
511 }
John W. Linvillef2223132006-01-23 16:59:58 -0500512}
513
Michael Buesch77db31e2006-02-12 16:47:44 +0100514void bcm43xx_pio_rx(struct bcm43xx_pioqueue *queue)
John W. Linvillef2223132006-01-23 16:59:58 -0500515{
516 u16 preamble[21] = { 0 };
517 struct bcm43xx_rxhdr *rxhdr;
Michael Buesch77db31e2006-02-12 16:47:44 +0100518 u16 tmp, len, rxflags2;
519 int i, preamble_readwords;
John W. Linvillef2223132006-01-23 16:59:58 -0500520 struct sk_buff *skb;
521
Michael Buesch77db31e2006-02-12 16:47:44 +0100522return;
John W. Linvillef2223132006-01-23 16:59:58 -0500523 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXCTL);
524 if (!(tmp & BCM43xx_PIO_RXCTL_DATAAVAILABLE)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100525 dprintkl(KERN_ERR PFX "PIO RX: No data available\n");//TODO: remove this printk.
John W. Linvillef2223132006-01-23 16:59:58 -0500526 return;
527 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100528 bcm43xx_pio_write(queue, BCM43xx_PIO_RXCTL,
529 BCM43xx_PIO_RXCTL_DATAAVAILABLE);
John W. Linvillef2223132006-01-23 16:59:58 -0500530
531 for (i = 0; i < 10; i++) {
532 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXCTL);
533 if (tmp & BCM43xx_PIO_RXCTL_READY)
534 goto data_ready;
535 udelay(10);
536 }
537 dprintkl(KERN_ERR PFX "PIO RX timed out\n");
538 return;
539data_ready:
540
Michael Buesch77db31e2006-02-12 16:47:44 +0100541//FIXME: endianess in this function.
John W. Linvillef2223132006-01-23 16:59:58 -0500542 len = le16_to_cpu(bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA));
543 if (unlikely(len > 0x700)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100544 pio_rx_error(queue, 0, "len > 0x700");
John W. Linvillef2223132006-01-23 16:59:58 -0500545 return;
546 }
547 if (unlikely(len == 0 && queue->mmio_base != BCM43xx_MMIO_PIO4_BASE)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100548 pio_rx_error(queue, 0, "len == 0");
John W. Linvillef2223132006-01-23 16:59:58 -0500549 return;
550 }
551 preamble[0] = cpu_to_le16(len);
552 if (queue->mmio_base == BCM43xx_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 = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
Michael Buesch77db31e2006-02-12 16:47:44 +0100558 preamble[i + 1] = cpu_to_be16(tmp);//FIXME?
John W. Linvillef2223132006-01-23 16:59:58 -0500559 }
560 rxhdr = (struct bcm43xx_rxhdr *)preamble;
Michael Buesch77db31e2006-02-12 16:47:44 +0100561 rxflags2 = le16_to_cpu(rxhdr->flags2);
562 if (unlikely(rxflags2 & BCM43xx_RXHDR_FLAGS2_INVALIDFRAME)) {
563 pio_rx_error(queue,
564 (queue->mmio_base == BCM43xx_MMIO_PIO1_BASE),
565 "invalid frame");
John W. Linvillef2223132006-01-23 16:59:58 -0500566 return;
567 }
John W. Linvillef2223132006-01-23 16:59:58 -0500568 if (queue->mmio_base == BCM43xx_MMIO_PIO4_BASE) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100569 /* We received an xmit status. */
570 struct bcm43xx_hwxmitstatus *hw;
571 struct bcm43xx_xmitstatus stat;
572
573 hw = (struct bcm43xx_hwxmitstatus *)(preamble + 1);
574 stat.cookie = le16_to_cpu(hw->cookie);
575 stat.flags = hw->flags;
576 stat.cnt1 = hw->cnt1;
577 stat.cnt2 = hw->cnt2;
578 stat.seq = le16_to_cpu(hw->seq);
579 stat.unknown = le16_to_cpu(hw->unknown);
580
581 bcm43xx_debugfs_log_txstat(queue->bcm, &stat);
582 bcm43xx_pio_handle_xmitstatus(queue->bcm, &stat);
583
John W. Linvillef2223132006-01-23 16:59:58 -0500584 return;
585 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100586
John W. Linvillef2223132006-01-23 16:59:58 -0500587 skb = dev_alloc_skb(len);
588 if (unlikely(!skb)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100589 pio_rx_error(queue, 1, "OOM");
John W. Linvillef2223132006-01-23 16:59:58 -0500590 return;
591 }
592 skb_put(skb, len);
593 for (i = 0; i < len - 1; i += 2) {
594 tmp = cpu_to_be16(bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA));
595 *((u16 *)(skb->data + i)) = tmp;
596 }
597 if (len % 2) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100598 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
John W. Linvillef2223132006-01-23 16:59:58 -0500599 skb->data[len - 1] = (tmp & 0x00FF);
Michael Buesch77db31e2006-02-12 16:47:44 +0100600 if (rxflags2 & BCM43xx_RXHDR_FLAGS2_TYPE2FRAME)
601 skb->data[0x20] = (tmp & 0xFF00) >> 8;
602 else
603 skb->data[0x1E] = (tmp & 0xFF00) >> 8;
John W. Linvillef2223132006-01-23 16:59:58 -0500604 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100605 bcm43xx_rx(queue->bcm, skb, rxhdr);
John W. Linvillef2223132006-01-23 16:59:58 -0500606}