blob: 0aa1bd269a25361f07ff858ada77bb5e68dc7b7f [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"
Michael Buesch7c241d32006-04-23 13:23:10 +020030#include "bcm43xx_power.h"
John W. Linvillef2223132006-01-23 16:59:58 -050031
32#include <linux/delay.h>
33
34
Michael Buesch77db31e2006-02-12 16:47:44 +010035static void tx_start(struct bcm43xx_pioqueue *queue)
John W. Linvillef2223132006-01-23 16:59:58 -050036{
Michael Buesch77db31e2006-02-12 16:47:44 +010037 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
38 BCM43xx_PIO_TXCTL_INIT);
John W. Linvillef2223132006-01-23 16:59:58 -050039}
40
Michael Buesch77db31e2006-02-12 16:47:44 +010041static void tx_octet(struct bcm43xx_pioqueue *queue,
42 u8 octet)
John W. Linvillef2223132006-01-23 16:59:58 -050043{
Michael Buesch77db31e2006-02-12 16:47:44 +010044 if (queue->need_workarounds) {
45 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
46 octet);
47 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch7c241d32006-04-23 13:23:10 +020048 BCM43xx_PIO_TXCTL_WRITELO);
John W. Linvillef2223132006-01-23 16:59:58 -050049 } else {
Michael Buesch77db31e2006-02-12 16:47:44 +010050 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch7c241d32006-04-23 13:23:10 +020051 BCM43xx_PIO_TXCTL_WRITELO);
Michael Buesch77db31e2006-02-12 16:47:44 +010052 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
53 octet);
John W. Linvillef2223132006-01-23 16:59:58 -050054 }
55}
56
Michael Buesch77db31e2006-02-12 16:47:44 +010057static u16 tx_get_next_word(struct bcm43xx_txhdr *txhdr,
58 const u8 *packet,
59 unsigned int *pos)
60{
61 const u8 *source;
62 unsigned int i = *pos;
63 u16 ret;
64
65 if (i < sizeof(*txhdr)) {
66 source = (const u8 *)txhdr;
67 } else {
68 source = packet;
69 i -= sizeof(*txhdr);
70 }
71 ret = le16_to_cpu( *((u16 *)(source + i)) );
72 *pos += 2;
73
74 return ret;
75}
76
77static void tx_data(struct bcm43xx_pioqueue *queue,
78 struct bcm43xx_txhdr *txhdr,
79 const u8 *packet,
80 unsigned int octets)
John W. Linvillef2223132006-01-23 16:59:58 -050081{
82 u16 data;
83 unsigned int i = 0;
84
Michael Buesch77db31e2006-02-12 16:47:44 +010085 if (queue->need_workarounds) {
86 data = tx_get_next_word(txhdr, packet, &i);
John W. Linvillef2223132006-01-23 16:59:58 -050087 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA, data);
John W. Linvillef2223132006-01-23 16:59:58 -050088 }
89 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch77db31e2006-02-12 16:47:44 +010090 BCM43xx_PIO_TXCTL_WRITELO |
91 BCM43xx_PIO_TXCTL_WRITEHI);
92 while (i < octets - 1) {
93 data = tx_get_next_word(txhdr, packet, &i);
John W. Linvillef2223132006-01-23 16:59:58 -050094 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA, data);
95 }
96 if (octets % 2)
Michael Buesch77db31e2006-02-12 16:47:44 +010097 tx_octet(queue, packet[octets - sizeof(*txhdr) - 1]);
John W. Linvillef2223132006-01-23 16:59:58 -050098}
99
Michael Buesch77db31e2006-02-12 16:47:44 +0100100static void tx_complete(struct bcm43xx_pioqueue *queue,
101 struct sk_buff *skb)
John W. Linvillef2223132006-01-23 16:59:58 -0500102{
Michael Buesch77db31e2006-02-12 16:47:44 +0100103 if (queue->need_workarounds) {
104 bcm43xx_pio_write(queue, BCM43xx_PIO_TXDATA,
105 skb->data[skb->len - 1]);
John W. Linvillef2223132006-01-23 16:59:58 -0500106 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
Michael Buesch7c241d32006-04-23 13:23:10 +0200107 BCM43xx_PIO_TXCTL_WRITELO |
Michael Buesch77db31e2006-02-12 16:47:44 +0100108 BCM43xx_PIO_TXCTL_COMPLETE);
John W. Linvillef2223132006-01-23 16:59:58 -0500109 } else {
Michael Buesch77db31e2006-02-12 16:47:44 +0100110 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
111 BCM43xx_PIO_TXCTL_COMPLETE);
John W. Linvillef2223132006-01-23 16:59:58 -0500112 }
113}
114
Michael Buesch77db31e2006-02-12 16:47:44 +0100115static u16 generate_cookie(struct bcm43xx_pioqueue *queue,
Michael Buesch7c241d32006-04-23 13:23:10 +0200116 struct bcm43xx_pio_txpacket *packet)
John W. Linvillef2223132006-01-23 16:59:58 -0500117{
118 u16 cookie = 0x0000;
Michael Buesch7c241d32006-04-23 13:23:10 +0200119 int packetindex;
John W. Linvillef2223132006-01-23 16:59:58 -0500120
121 /* We use the upper 4 bits for the PIO
122 * controller ID and the lower 12 bits
123 * for the packet index (in the cache).
124 */
125 switch (queue->mmio_base) {
John W. Linvillef2223132006-01-23 16:59:58 -0500126 case BCM43xx_MMIO_PIO1_BASE:
127 break;
128 case BCM43xx_MMIO_PIO2_BASE:
129 cookie = 0x1000;
130 break;
131 case BCM43xx_MMIO_PIO3_BASE:
132 cookie = 0x2000;
133 break;
134 case BCM43xx_MMIO_PIO4_BASE:
135 cookie = 0x3000;
136 break;
Michael Buesch77db31e2006-02-12 16:47:44 +0100137 default:
138 assert(0);
John W. Linvillef2223132006-01-23 16:59:58 -0500139 }
Michael Buesch7c241d32006-04-23 13:23:10 +0200140 packetindex = pio_txpacket_getindex(packet);
John W. Linvillef2223132006-01-23 16:59:58 -0500141 assert(((u16)packetindex & 0xF000) == 0x0000);
142 cookie |= (u16)packetindex;
143
144 return cookie;
145}
146
Michael Buesch77db31e2006-02-12 16:47:44 +0100147static
John W. Linvillef2223132006-01-23 16:59:58 -0500148struct bcm43xx_pioqueue * parse_cookie(struct bcm43xx_private *bcm,
149 u16 cookie,
150 struct bcm43xx_pio_txpacket **packet)
151{
Michael Buesche9357c02006-03-13 19:27:34 +0100152 struct bcm43xx_pio *pio = bcm43xx_current_pio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500153 struct bcm43xx_pioqueue *queue = NULL;
154 int packetindex;
155
156 switch (cookie & 0xF000) {
157 case 0x0000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100158 queue = pio->queue0;
John W. Linvillef2223132006-01-23 16:59:58 -0500159 break;
160 case 0x1000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100161 queue = pio->queue1;
John W. Linvillef2223132006-01-23 16:59:58 -0500162 break;
163 case 0x2000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100164 queue = pio->queue2;
John W. Linvillef2223132006-01-23 16:59:58 -0500165 break;
166 case 0x3000:
Michael Buesch77db31e2006-02-12 16:47:44 +0100167 queue = pio->queue3;
John W. Linvillef2223132006-01-23 16:59:58 -0500168 break;
169 default:
170 assert(0);
171 }
John W. Linvillef2223132006-01-23 16:59:58 -0500172 packetindex = (cookie & 0x0FFF);
173 assert(packetindex >= 0 && packetindex < BCM43xx_PIO_MAXTXPACKETS);
Michael Buesch77db31e2006-02-12 16:47:44 +0100174 *packet = &(queue->tx_packets_cache[packetindex]);
John W. Linvillef2223132006-01-23 16:59:58 -0500175
176 return queue;
177}
178
Michael Buesch77db31e2006-02-12 16:47:44 +0100179static void pio_tx_write_fragment(struct bcm43xx_pioqueue *queue,
180 struct sk_buff *skb,
181 struct bcm43xx_pio_txpacket *packet)
John W. Linvillef2223132006-01-23 16:59:58 -0500182{
Michael Buesch77db31e2006-02-12 16:47:44 +0100183 struct bcm43xx_txhdr txhdr;
John W. Linvillef2223132006-01-23 16:59:58 -0500184 unsigned int octets;
185
186 assert(skb_shinfo(skb)->nr_frags == 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500187 bcm43xx_generate_txhdr(queue->bcm,
Michael Buesch77db31e2006-02-12 16:47:44 +0100188 &txhdr, skb->data, skb->len,
John W. Linvillef2223132006-01-23 16:59:58 -0500189 (packet->xmitted_frags == 0),
Michael Buesch7c241d32006-04-23 13:23:10 +0200190 generate_cookie(queue, packet));
John W. Linvillef2223132006-01-23 16:59:58 -0500191
192 tx_start(queue);
Michael Buesch77db31e2006-02-12 16:47:44 +0100193 octets = skb->len + sizeof(txhdr);
194 if (queue->need_workarounds)
195 octets--;
196 tx_data(queue, &txhdr, (u8 *)skb->data, octets);
John W. Linvillef2223132006-01-23 16:59:58 -0500197 tx_complete(queue, skb);
198}
199
Michael Buesch77db31e2006-02-12 16:47:44 +0100200static void free_txpacket(struct bcm43xx_pio_txpacket *packet,
201 int irq_context)
202{
203 struct bcm43xx_pioqueue *queue = packet->queue;
204
205 ieee80211_txb_free(packet->txb);
206 list_move(&packet->list, &queue->txfree);
207 queue->nr_txfree++;
208
209 assert(queue->tx_devq_used >= packet->xmitted_octets);
210 assert(queue->tx_devq_packets >= packet->xmitted_frags);
211 queue->tx_devq_used -= packet->xmitted_octets;
212 queue->tx_devq_packets -= packet->xmitted_frags;
213}
214
215static int pio_tx_packet(struct bcm43xx_pio_txpacket *packet)
John W. Linvillef2223132006-01-23 16:59:58 -0500216{
217 struct bcm43xx_pioqueue *queue = packet->queue;
218 struct ieee80211_txb *txb = packet->txb;
219 struct sk_buff *skb;
220 u16 octets;
221 int i;
222
223 for (i = packet->xmitted_frags; i < txb->nr_frags; i++) {
224 skb = txb->fragments[i];
225
226 octets = (u16)skb->len + sizeof(struct bcm43xx_txhdr);
John W. Linvillef2223132006-01-23 16:59:58 -0500227 assert(queue->tx_devq_size >= octets);
228 assert(queue->tx_devq_packets <= BCM43xx_PIO_MAXTXDEVQPACKETS);
229 assert(queue->tx_devq_used <= queue->tx_devq_size);
230 /* Check if there is sufficient free space on the device
Michael Buesch77db31e2006-02-12 16:47:44 +0100231 * TX queue. If not, return and let the TX tasklet
John W. Linvillef2223132006-01-23 16:59:58 -0500232 * retry later.
233 */
234 if (queue->tx_devq_packets == BCM43xx_PIO_MAXTXDEVQPACKETS)
235 return -EBUSY;
236 if (queue->tx_devq_used + octets > queue->tx_devq_size)
237 return -EBUSY;
238 /* Now poke the device. */
239 pio_tx_write_fragment(queue, skb, packet);
240
241 /* Account for the packet size.
242 * (We must not overflow the device TX queue)
243 */
244 queue->tx_devq_packets++;
245 queue->tx_devq_used += octets;
246
Michael Buesch7c241d32006-04-23 13:23:10 +0200247 assert(packet->xmitted_frags < packet->txb->nr_frags);
John W. Linvillef2223132006-01-23 16:59:58 -0500248 packet->xmitted_frags++;
249 packet->xmitted_octets += octets;
250 }
251 list_move_tail(&packet->list, &queue->txrunning);
252
253 return 0;
254}
255
Michael Buesch77db31e2006-02-12 16:47:44 +0100256static void tx_tasklet(unsigned long d)
John W. Linvillef2223132006-01-23 16:59:58 -0500257{
Michael Buesch77db31e2006-02-12 16:47:44 +0100258 struct bcm43xx_pioqueue *queue = (struct bcm43xx_pioqueue *)d;
259 struct bcm43xx_private *bcm = queue->bcm;
John W. Linvillef2223132006-01-23 16:59:58 -0500260 unsigned long flags;
261 struct bcm43xx_pio_txpacket *packet, *tmp_packet;
262 int err;
Michael Buesch7c241d32006-04-23 13:23:10 +0200263 u16 txctl;
John W. Linvillef2223132006-01-23 16:59:58 -0500264
Michael Bueschefccb642006-03-11 13:39:14 +0100265 bcm43xx_lock_mmio(bcm, flags);
Michael Buesch7c241d32006-04-23 13:23:10 +0200266
267 txctl = bcm43xx_pio_read(queue, BCM43xx_PIO_TXCTL);
268 if (txctl & BCM43xx_PIO_TXCTL_SUSPEND)
269 goto out_unlock;
270
John W. Linvillef2223132006-01-23 16:59:58 -0500271 list_for_each_entry_safe(packet, tmp_packet, &queue->txqueue, list) {
272 assert(packet->xmitted_frags < packet->txb->nr_frags);
273 if (packet->xmitted_frags == 0) {
274 int i;
275 struct sk_buff *skb;
276
277 /* Check if the device queue is big
278 * enough for every fragment. If not, drop the
279 * whole packet.
280 */
281 for (i = 0; i < packet->txb->nr_frags; i++) {
282 skb = packet->txb->fragments[i];
283 if (unlikely(skb->len > queue->tx_devq_size)) {
284 dprintkl(KERN_ERR PFX "PIO TX device queue too small. "
Michael Buesch77db31e2006-02-12 16:47:44 +0100285 "Dropping packet.\n");
286 free_txpacket(packet, 1);
John W. Linvillef2223132006-01-23 16:59:58 -0500287 goto next_packet;
288 }
289 }
290 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100291 /* Try to transmit the packet.
John W. Linvillef2223132006-01-23 16:59:58 -0500292 * This may not completely succeed.
293 */
294 err = pio_tx_packet(packet);
295 if (err)
296 break;
Michael Buesch77db31e2006-02-12 16:47:44 +0100297 next_packet:
John W. Linvillef2223132006-01-23 16:59:58 -0500298 continue;
299 }
Michael Buesch7c241d32006-04-23 13:23:10 +0200300out_unlock:
Michael Bueschefccb642006-03-11 13:39:14 +0100301 bcm43xx_unlock_mmio(bcm, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500302}
303
304static void setup_txqueues(struct bcm43xx_pioqueue *queue)
305{
306 struct bcm43xx_pio_txpacket *packet;
307 int i;
308
Michael Buesch77db31e2006-02-12 16:47:44 +0100309 queue->nr_txfree = BCM43xx_PIO_MAXTXPACKETS;
John W. Linvillef2223132006-01-23 16:59:58 -0500310 for (i = 0; i < BCM43xx_PIO_MAXTXPACKETS; i++) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100311 packet = &(queue->tx_packets_cache[i]);
John W. Linvillef2223132006-01-23 16:59:58 -0500312
313 packet->queue = queue;
314 INIT_LIST_HEAD(&packet->list);
315
316 list_add(&packet->list, &queue->txfree);
317 }
318}
319
320static
321struct bcm43xx_pioqueue * bcm43xx_setup_pioqueue(struct bcm43xx_private *bcm,
322 u16 pio_mmio_base)
323{
324 struct bcm43xx_pioqueue *queue;
325 u32 value;
326 u16 qsize;
327
Michael Buesch77db31e2006-02-12 16:47:44 +0100328 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
John W. Linvillef2223132006-01-23 16:59:58 -0500329 if (!queue)
330 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -0500331
332 queue->bcm = bcm;
333 queue->mmio_base = pio_mmio_base;
Michael Buesch77db31e2006-02-12 16:47:44 +0100334 queue->need_workarounds = (bcm->current_core->rev < 3);
John W. Linvillef2223132006-01-23 16:59:58 -0500335
336 INIT_LIST_HEAD(&queue->txfree);
337 INIT_LIST_HEAD(&queue->txqueue);
338 INIT_LIST_HEAD(&queue->txrunning);
Michael Buesch77db31e2006-02-12 16:47:44 +0100339 tasklet_init(&queue->txtask, tx_tasklet,
340 (unsigned long)queue);
John W. Linvillef2223132006-01-23 16:59:58 -0500341
342 value = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
Michael Buesch7c241d32006-04-23 13:23:10 +0200343 value &= ~BCM43xx_SBF_XFER_REG_BYTESWAP;
John W. Linvillef2223132006-01-23 16:59:58 -0500344 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, value);
345
346 qsize = bcm43xx_read16(bcm, queue->mmio_base + BCM43xx_PIO_TXQBUFSIZE);
Michael Buesch7c241d32006-04-23 13:23:10 +0200347 if (qsize == 0) {
348 printk(KERN_ERR PFX "ERROR: This card does not support PIO "
349 "operation mode. Please use DMA mode "
350 "(module parameter pio=0).\n");
351 goto err_freequeue;
352 }
John W. Linvillef2223132006-01-23 16:59:58 -0500353 if (qsize <= BCM43xx_PIO_TXQADJUST) {
Michael Buesch7c241d32006-04-23 13:23:10 +0200354 printk(KERN_ERR PFX "PIO tx device-queue too small (%u)\n",
355 qsize);
John W. Linvillef2223132006-01-23 16:59:58 -0500356 goto err_freequeue;
357 }
358 qsize -= BCM43xx_PIO_TXQADJUST;
359 queue->tx_devq_size = qsize;
360
361 setup_txqueues(queue);
362
363out:
364 return queue;
365
366err_freequeue:
367 kfree(queue);
368 queue = NULL;
369 goto out;
370}
371
372static void cancel_transfers(struct bcm43xx_pioqueue *queue)
373{
374 struct bcm43xx_pio_txpacket *packet, *tmp_packet;
375
376 netif_tx_disable(queue->bcm->net_dev);
377 assert(queue->bcm->shutting_down);
Michael Buesch77db31e2006-02-12 16:47:44 +0100378 tasklet_disable(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500379
380 list_for_each_entry_safe(packet, tmp_packet, &queue->txrunning, list)
Michael Buesch77db31e2006-02-12 16:47:44 +0100381 free_txpacket(packet, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500382 list_for_each_entry_safe(packet, tmp_packet, &queue->txqueue, list)
Michael Buesch77db31e2006-02-12 16:47:44 +0100383 free_txpacket(packet, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500384}
385
386static void bcm43xx_destroy_pioqueue(struct bcm43xx_pioqueue *queue)
387{
388 if (!queue)
389 return;
390
391 cancel_transfers(queue);
392 kfree(queue);
393}
394
395void bcm43xx_pio_free(struct bcm43xx_private *bcm)
396{
Michael Buesch49f29efa2006-03-14 16:05:26 +0100397 struct bcm43xx_pio *pio;
398
399 if (!bcm43xx_using_pio(bcm))
400 return;
401 pio = bcm43xx_current_pio(bcm);
Michael Buesch77db31e2006-02-12 16:47:44 +0100402
403 bcm43xx_destroy_pioqueue(pio->queue3);
404 pio->queue3 = NULL;
405 bcm43xx_destroy_pioqueue(pio->queue2);
406 pio->queue2 = NULL;
407 bcm43xx_destroy_pioqueue(pio->queue1);
408 pio->queue1 = NULL;
409 bcm43xx_destroy_pioqueue(pio->queue0);
410 pio->queue0 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500411}
412
413int bcm43xx_pio_init(struct bcm43xx_private *bcm)
414{
Michael Buesche9357c02006-03-13 19:27:34 +0100415 struct bcm43xx_pio *pio = bcm43xx_current_pio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500416 struct bcm43xx_pioqueue *queue;
417 int err = -ENOMEM;
418
419 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO1_BASE);
420 if (!queue)
421 goto out;
Michael Buesch77db31e2006-02-12 16:47:44 +0100422 pio->queue0 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500423
424 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO2_BASE);
425 if (!queue)
426 goto err_destroy0;
Michael Buesch77db31e2006-02-12 16:47:44 +0100427 pio->queue1 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500428
429 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO3_BASE);
430 if (!queue)
431 goto err_destroy1;
Michael Buesch77db31e2006-02-12 16:47:44 +0100432 pio->queue2 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500433
434 queue = bcm43xx_setup_pioqueue(bcm, BCM43xx_MMIO_PIO4_BASE);
435 if (!queue)
436 goto err_destroy2;
Michael Buesch77db31e2006-02-12 16:47:44 +0100437 pio->queue3 = queue;
John W. Linvillef2223132006-01-23 16:59:58 -0500438
439 if (bcm->current_core->rev < 3)
440 bcm->irq_savedstate |= BCM43xx_IRQ_PIO_WORKAROUND;
441
442 dprintk(KERN_INFO PFX "PIO initialized\n");
443 err = 0;
444out:
445 return err;
446
447err_destroy2:
Michael Buesch77db31e2006-02-12 16:47:44 +0100448 bcm43xx_destroy_pioqueue(pio->queue2);
449 pio->queue2 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500450err_destroy1:
Michael Buesch77db31e2006-02-12 16:47:44 +0100451 bcm43xx_destroy_pioqueue(pio->queue1);
452 pio->queue1 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500453err_destroy0:
Michael Buesch77db31e2006-02-12 16:47:44 +0100454 bcm43xx_destroy_pioqueue(pio->queue0);
455 pio->queue0 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500456 goto out;
457}
458
Michael Buesch77db31e2006-02-12 16:47:44 +0100459int bcm43xx_pio_tx(struct bcm43xx_private *bcm,
460 struct ieee80211_txb *txb)
John W. Linvillef2223132006-01-23 16:59:58 -0500461{
Michael Buesche9357c02006-03-13 19:27:34 +0100462 struct bcm43xx_pioqueue *queue = bcm43xx_current_pio(bcm)->queue1;
John W. Linvillef2223132006-01-23 16:59:58 -0500463 struct bcm43xx_pio_txpacket *packet;
John W. Linvillef2223132006-01-23 16:59:58 -0500464
John W. Linvillef2223132006-01-23 16:59:58 -0500465 assert(!queue->tx_suspended);
466 assert(!list_empty(&queue->txfree));
467
John W. Linvillef2223132006-01-23 16:59:58 -0500468 packet = list_entry(queue->txfree.next, struct bcm43xx_pio_txpacket, list);
John W. Linvillef2223132006-01-23 16:59:58 -0500469 packet->txb = txb;
John W. Linvillef2223132006-01-23 16:59:58 -0500470 packet->xmitted_frags = 0;
Michael Buesch77db31e2006-02-12 16:47:44 +0100471 packet->xmitted_octets = 0;
472 list_move_tail(&packet->list, &queue->txqueue);
473 queue->nr_txfree--;
474 assert(queue->nr_txfree < BCM43xx_PIO_MAXTXPACKETS);
John W. Linvillef2223132006-01-23 16:59:58 -0500475
476 /* Suspend TX, if we are out of packets in the "free" queue. */
Michael Buesch7c241d32006-04-23 13:23:10 +0200477 if (list_empty(&queue->txfree)) {
John W. Linvillef2223132006-01-23 16:59:58 -0500478 netif_stop_queue(queue->bcm->net_dev);
479 queue->tx_suspended = 1;
480 }
481
Michael Buesch77db31e2006-02-12 16:47:44 +0100482 tasklet_schedule(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500483
484 return 0;
485}
486
Michael Buesch77db31e2006-02-12 16:47:44 +0100487void bcm43xx_pio_handle_xmitstatus(struct bcm43xx_private *bcm,
488 struct bcm43xx_xmitstatus *status)
John W. Linvillef2223132006-01-23 16:59:58 -0500489{
490 struct bcm43xx_pioqueue *queue;
491 struct bcm43xx_pio_txpacket *packet;
John W. Linvillef2223132006-01-23 16:59:58 -0500492
493 queue = parse_cookie(bcm, status->cookie, &packet);
494 assert(queue);
Michael Buesch7c241d32006-04-23 13:23:10 +0200495
Michael Buesch77db31e2006-02-12 16:47:44 +0100496 free_txpacket(packet, 1);
Michael Buesch7c241d32006-04-23 13:23:10 +0200497 if (queue->tx_suspended) {
John W. Linvillef2223132006-01-23 16:59:58 -0500498 queue->tx_suspended = 0;
499 netif_wake_queue(queue->bcm->net_dev);
500 }
Michael Buesch7c241d32006-04-23 13:23:10 +0200501 /* If there are packets on the txqueue, poke the tasklet
502 * to transmit them.
503 */
Michael Buesch77db31e2006-02-12 16:47:44 +0100504 if (!list_empty(&queue->txqueue))
505 tasklet_schedule(&queue->txtask);
John W. Linvillef2223132006-01-23 16:59:58 -0500506}
507
508static void pio_rx_error(struct bcm43xx_pioqueue *queue,
Michael Buesch77db31e2006-02-12 16:47:44 +0100509 int clear_buffers,
John W. Linvillef2223132006-01-23 16:59:58 -0500510 const char *error)
511{
Michael Buesch77db31e2006-02-12 16:47:44 +0100512 int i;
513
514 printkl("PIO RX error: %s\n", error);
515 bcm43xx_pio_write(queue, BCM43xx_PIO_RXCTL,
516 BCM43xx_PIO_RXCTL_READY);
517 if (clear_buffers) {
518 assert(queue->mmio_base == BCM43xx_MMIO_PIO1_BASE);
519 for (i = 0; i < 15; i++) {
520 /* Dummy read. */
521 bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
522 }
523 }
John W. Linvillef2223132006-01-23 16:59:58 -0500524}
525
Michael Buesch77db31e2006-02-12 16:47:44 +0100526void bcm43xx_pio_rx(struct bcm43xx_pioqueue *queue)
John W. Linvillef2223132006-01-23 16:59:58 -0500527{
528 u16 preamble[21] = { 0 };
529 struct bcm43xx_rxhdr *rxhdr;
Michael Buesch77db31e2006-02-12 16:47:44 +0100530 u16 tmp, len, rxflags2;
531 int i, preamble_readwords;
John W. Linvillef2223132006-01-23 16:59:58 -0500532 struct sk_buff *skb;
533
534 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXCTL);
Michael Buesch7c241d32006-04-23 13:23:10 +0200535 if (!(tmp & BCM43xx_PIO_RXCTL_DATAAVAILABLE))
John W. Linvillef2223132006-01-23 16:59:58 -0500536 return;
Michael Buesch77db31e2006-02-12 16:47:44 +0100537 bcm43xx_pio_write(queue, BCM43xx_PIO_RXCTL,
538 BCM43xx_PIO_RXCTL_DATAAVAILABLE);
John W. Linvillef2223132006-01-23 16:59:58 -0500539
540 for (i = 0; i < 10; i++) {
541 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXCTL);
542 if (tmp & BCM43xx_PIO_RXCTL_READY)
543 goto data_ready;
544 udelay(10);
545 }
546 dprintkl(KERN_ERR PFX "PIO RX timed out\n");
547 return;
548data_ready:
549
Michael Buesch7c241d32006-04-23 13:23:10 +0200550 len = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
John W. Linvillef2223132006-01-23 16:59:58 -0500551 if (unlikely(len > 0x700)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100552 pio_rx_error(queue, 0, "len > 0x700");
John W. Linvillef2223132006-01-23 16:59:58 -0500553 return;
554 }
555 if (unlikely(len == 0 && queue->mmio_base != BCM43xx_MMIO_PIO4_BASE)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100556 pio_rx_error(queue, 0, "len == 0");
John W. Linvillef2223132006-01-23 16:59:58 -0500557 return;
558 }
559 preamble[0] = cpu_to_le16(len);
560 if (queue->mmio_base == BCM43xx_MMIO_PIO4_BASE)
561 preamble_readwords = 14 / sizeof(u16);
562 else
563 preamble_readwords = 18 / sizeof(u16);
564 for (i = 0; i < preamble_readwords; i++) {
565 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
Michael Buesch7c241d32006-04-23 13:23:10 +0200566 preamble[i + 1] = cpu_to_le16(tmp);
John W. Linvillef2223132006-01-23 16:59:58 -0500567 }
568 rxhdr = (struct bcm43xx_rxhdr *)preamble;
Michael Buesch77db31e2006-02-12 16:47:44 +0100569 rxflags2 = le16_to_cpu(rxhdr->flags2);
570 if (unlikely(rxflags2 & BCM43xx_RXHDR_FLAGS2_INVALIDFRAME)) {
571 pio_rx_error(queue,
572 (queue->mmio_base == BCM43xx_MMIO_PIO1_BASE),
573 "invalid frame");
John W. Linvillef2223132006-01-23 16:59:58 -0500574 return;
575 }
John W. Linvillef2223132006-01-23 16:59:58 -0500576 if (queue->mmio_base == BCM43xx_MMIO_PIO4_BASE) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100577 /* We received an xmit status. */
578 struct bcm43xx_hwxmitstatus *hw;
579 struct bcm43xx_xmitstatus stat;
580
581 hw = (struct bcm43xx_hwxmitstatus *)(preamble + 1);
582 stat.cookie = le16_to_cpu(hw->cookie);
583 stat.flags = hw->flags;
584 stat.cnt1 = hw->cnt1;
585 stat.cnt2 = hw->cnt2;
586 stat.seq = le16_to_cpu(hw->seq);
587 stat.unknown = le16_to_cpu(hw->unknown);
588
589 bcm43xx_debugfs_log_txstat(queue->bcm, &stat);
590 bcm43xx_pio_handle_xmitstatus(queue->bcm, &stat);
591
John W. Linvillef2223132006-01-23 16:59:58 -0500592 return;
593 }
Michael Buesch77db31e2006-02-12 16:47:44 +0100594
John W. Linvillef2223132006-01-23 16:59:58 -0500595 skb = dev_alloc_skb(len);
596 if (unlikely(!skb)) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100597 pio_rx_error(queue, 1, "OOM");
John W. Linvillef2223132006-01-23 16:59:58 -0500598 return;
599 }
600 skb_put(skb, len);
601 for (i = 0; i < len - 1; i += 2) {
Michael Buesch7c241d32006-04-23 13:23:10 +0200602 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
603 *((u16 *)(skb->data + i)) = cpu_to_le16(tmp);
John W. Linvillef2223132006-01-23 16:59:58 -0500604 }
605 if (len % 2) {
Michael Buesch77db31e2006-02-12 16:47:44 +0100606 tmp = bcm43xx_pio_read(queue, BCM43xx_PIO_RXDATA);
John W. Linvillef2223132006-01-23 16:59:58 -0500607 skb->data[len - 1] = (tmp & 0x00FF);
Michael Buesch7c241d32006-04-23 13:23:10 +0200608/* The specs say the following is required, but
609 * it is wrong and corrupts the PLCP. If we don't do
610 * this, the PLCP seems to be correct. So ifdef it out for now.
611 */
612#if 0
Michael Buesch77db31e2006-02-12 16:47:44 +0100613 if (rxflags2 & BCM43xx_RXHDR_FLAGS2_TYPE2FRAME)
Michael Buesch7c241d32006-04-23 13:23:10 +0200614 skb->data[2] = (tmp & 0xFF00) >> 8;
Michael Buesch77db31e2006-02-12 16:47:44 +0100615 else
Michael Buesch7c241d32006-04-23 13:23:10 +0200616 skb->data[0] = (tmp & 0xFF00) >> 8;
617#endif
John W. Linvillef2223132006-01-23 16:59:58 -0500618 }
Michael Buesch7c241d32006-04-23 13:23:10 +0200619 skb_trim(skb, len - IEEE80211_FCS_LEN);
Michael Buesch77db31e2006-02-12 16:47:44 +0100620 bcm43xx_rx(queue->bcm, skb, rxhdr);
John W. Linvillef2223132006-01-23 16:59:58 -0500621}
Michael Buesch7c241d32006-04-23 13:23:10 +0200622
623void bcm43xx_pio_tx_suspend(struct bcm43xx_pioqueue *queue)
624{
625 bcm43xx_power_saving_ctl_bits(queue->bcm, -1, 1);
626 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
627 bcm43xx_pio_read(queue, BCM43xx_PIO_TXCTL)
628 | BCM43xx_PIO_TXCTL_SUSPEND);
629}
630
631void bcm43xx_pio_tx_resume(struct bcm43xx_pioqueue *queue)
632{
633 bcm43xx_pio_write(queue, BCM43xx_PIO_TXCTL,
634 bcm43xx_pio_read(queue, BCM43xx_PIO_TXCTL)
635 & ~BCM43xx_PIO_TXCTL_SUSPEND);
636 bcm43xx_power_saving_ctl_bits(queue->bcm, -1, -1);
637 tasklet_schedule(&queue->txtask);
638}