vserver 1.9.5.x5
[linux-2.6.git] / drivers / net / wireless / prism54 / islpci_eth.c
1 /*
2  *  
3  *  Copyright (C) 2002 Intersil Americas Inc.
4  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <linux/version.h>
21 #include <linux/module.h>
22
23 #include <linux/pci.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/if_arp.h>
28
29 #include "prismcompat.h"
30 #include "isl_38xx.h"
31 #include "islpci_eth.h"
32 #include "islpci_mgt.h"
33 #include "oid_mgt.h"
34
35 /******************************************************************************
36     Network Interface functions
37 ******************************************************************************/
38 void
39 islpci_eth_cleanup_transmit(islpci_private *priv,
40                             isl38xx_control_block *control_block)
41 {
42         struct sk_buff *skb;
43         u32 index;
44
45         /* compare the control block read pointer with the free pointer */
46         while (priv->free_data_tx !=
47                le32_to_cpu(control_block->
48                            device_curr_frag[ISL38XX_CB_TX_DATA_LQ])) {
49                 /* read the index of the first fragment to be freed */
50                 index = priv->free_data_tx % ISL38XX_CB_TX_QSIZE;
51
52                 /* check for holes in the arrays caused by multi fragment frames 
53                  * searching for the last fragment of a frame */
54                 if (priv->pci_map_tx_address[index] != (dma_addr_t) NULL) {
55                         /* entry is the last fragment of a frame
56                          * free the skb structure and unmap pci memory */
57                         skb = priv->data_low_tx[index];
58
59 #if VERBOSE > SHOW_ERROR_MESSAGES
60                         DEBUG(SHOW_TRACING,
61                               "cleanup skb %p skb->data %p skb->len %u truesize %u\n ",
62                               skb, skb->data, skb->len, skb->truesize);
63 #endif
64
65                         pci_unmap_single(priv->pdev,
66                                          priv->pci_map_tx_address[index],
67                                          skb->len, PCI_DMA_TODEVICE);
68                         dev_kfree_skb_irq(skb);
69                         skb = NULL;
70                 }
71                 /* increment the free data low queue pointer */
72                 priv->free_data_tx++;
73         }
74 }
75
76 int
77 islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
78 {
79         islpci_private *priv = netdev_priv(ndev);
80         isl38xx_control_block *cb = priv->control_block;
81         u32 index;
82         dma_addr_t pci_map_address;
83         int frame_size;
84         isl38xx_fragment *fragment;
85         int offset;
86         struct sk_buff *newskb;
87         int newskb_offset;
88         unsigned long flags;
89         unsigned char wds_mac[6];
90         u32 curr_frag;
91         int err = 0;
92
93 #if VERBOSE > SHOW_ERROR_MESSAGES
94         DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit \n");
95 #endif
96
97         /* lock the driver code */
98         spin_lock_irqsave(&priv->slock, flags);
99
100         /* determine the amount of fragments needed to store the frame */
101
102         frame_size = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
103         if (init_wds)
104                 frame_size += 6;
105
106         /* check whether the destination queue has enough fragments for the frame */
107         curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ]);
108         if (unlikely(curr_frag - priv->free_data_tx >= ISL38XX_CB_TX_QSIZE)) {
109                 printk(KERN_ERR "%s: transmit device queue full when awake\n",
110                        ndev->name);
111                 netif_stop_queue(ndev);
112
113                 /* trigger the device */
114                 isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
115                                   ISL38XX_DEV_INT_REG);
116                 udelay(ISL38XX_WRITEIO_DELAY);
117
118                 err = -EBUSY;
119                 goto drop_free;
120         }
121         /* Check alignment and WDS frame formatting. The start of the packet should
122          * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
123          * and add WDS address information */
124         if (likely(((long) skb->data & 0x03) | init_wds)) {
125                 /* get the number of bytes to add and re-allign */
126                 offset = (4 - (long) skb->data) & 0x03;
127                 offset += init_wds ? 6 : 0;
128
129                 /* check whether the current skb can be used  */
130                 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
131                         unsigned char *src = skb->data;
132
133 #if VERBOSE > SHOW_ERROR_MESSAGES
134                         DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
135                               init_wds);
136 #endif
137
138                         /* align the buffer on 4-byte boundary */
139                         skb_reserve(skb, (4 - (long) skb->data) & 0x03);
140                         if (init_wds) {
141                                 /* wds requires an additional address field of 6 bytes */
142                                 skb_put(skb, 6);
143 #ifdef ISLPCI_ETH_DEBUG
144                                 printk("islpci_eth_transmit:wds_mac\n");
145 #endif
146                                 memmove(skb->data + 6, src, skb->len);
147                                 memcpy(skb->data, wds_mac, 6);
148                         } else {
149                                 memmove(skb->data, src, skb->len);
150                         }
151
152 #if VERBOSE > SHOW_ERROR_MESSAGES
153                         DEBUG(SHOW_TRACING, "memmove %p %p %i \n", skb->data,
154                               src, skb->len);
155 #endif
156                 } else {
157                         newskb =
158                             dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
159                         if (unlikely(newskb == NULL)) {
160                                 printk(KERN_ERR "%s: Cannot allocate skb\n",
161                                        ndev->name);
162                                 err = -ENOMEM;
163                                 goto drop_free;
164                         }
165                         newskb_offset = (4 - (long) newskb->data) & 0x03;
166
167                         /* Check if newskb->data is aligned */
168                         if (newskb_offset)
169                                 skb_reserve(newskb, newskb_offset);
170
171                         skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
172                         if (init_wds) {
173                                 memcpy(newskb->data + 6, skb->data, skb->len);
174                                 memcpy(newskb->data, wds_mac, 6);
175 #ifdef ISLPCI_ETH_DEBUG
176                                 printk("islpci_eth_transmit:wds_mac\n");
177 #endif
178                         } else
179                                 memcpy(newskb->data, skb->data, skb->len);
180
181 #if VERBOSE > SHOW_ERROR_MESSAGES
182                         DEBUG(SHOW_TRACING, "memcpy %p %p %i wds %i\n",
183                               newskb->data, skb->data, skb->len, init_wds);
184 #endif
185
186                         newskb->dev = skb->dev;
187                         dev_kfree_skb(skb);
188                         skb = newskb;
189                 }
190         }
191         /* display the buffer contents for debugging */
192 #if VERBOSE > SHOW_ERROR_MESSAGES
193         DEBUG(SHOW_BUFFER_CONTENTS, "\ntx %p ", skb->data);
194         display_buffer((char *) skb->data, skb->len);
195 #endif
196
197         /* map the skb buffer to pci memory for DMA operation */
198         pci_map_address = pci_map_single(priv->pdev,
199                                          (void *) skb->data, skb->len,
200                                          PCI_DMA_TODEVICE);
201         if (unlikely(pci_map_address == 0)) {
202                 printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
203                        ndev->name);
204
205                 err = -EIO;
206                 goto drop_free;
207         }
208         /* Place the fragment in the control block structure. */
209         index = curr_frag % ISL38XX_CB_TX_QSIZE;
210         fragment = &cb->tx_data_low[index];
211
212         priv->pci_map_tx_address[index] = pci_map_address;
213         /* store the skb address for future freeing  */
214         priv->data_low_tx[index] = skb;
215         /* set the proper fragment start address and size information */
216         fragment->size = cpu_to_le16(frame_size);
217         fragment->flags = cpu_to_le16(0);       /* set to 1 if more fragments */
218         fragment->address = cpu_to_le32(pci_map_address);
219         curr_frag++;
220
221         /* The fragment address in the control block must have been
222          * written before announcing the frame buffer to device. */
223         wmb();
224         cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
225
226         if (curr_frag - priv->free_data_tx + ISL38XX_MIN_QTHRESHOLD
227             > ISL38XX_CB_TX_QSIZE) {
228                 /* stop sends from upper layers */
229                 netif_stop_queue(ndev);
230
231                 /* set the full flag for the transmission queue */
232                 priv->data_low_tx_full = 1;
233         }
234
235         /* trigger the device */
236         islpci_trigger(priv);
237
238         /* unlock the driver code */
239         spin_unlock_irqrestore(&priv->slock, flags);
240
241         /* set the transmission time */
242         ndev->trans_start = jiffies;
243         priv->statistics.tx_packets++;
244         priv->statistics.tx_bytes += skb->len;
245
246         return 0;
247
248       drop_free:
249         /* free the skbuf structure before aborting */
250         dev_kfree_skb(skb);
251         skb = NULL;
252
253         priv->statistics.tx_dropped++;
254         spin_unlock_irqrestore(&priv->slock, flags);
255         return err;
256 }
257
258 static inline int
259 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
260 {
261         /* The card reports full 802.11 packets but with a 20 bytes
262          * header and without the FCS. But there a is a bit that
263          * indicates if the packet is corrupted :-) */
264         struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
265         if (hdr->flags & 0x01)
266                 /* This one is bad. Drop it ! */
267                 return -1;
268         if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
269                 struct avs_80211_1_header *avs;
270                 /* extract the relevant data from the header */
271                 u32 clock = le32_to_cpu(hdr->clock);
272                 u8 rate = hdr->rate;
273                 u16 freq = le16_to_cpu(hdr->freq);
274                 u8 rssi = hdr->rssi;
275
276                 skb_pull(*skb, sizeof (struct rfmon_header));
277
278                 if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
279                         struct sk_buff *newskb = skb_copy_expand(*skb,
280                                                                  sizeof (struct
281                                                                          avs_80211_1_header),
282                                                                  0, GFP_ATOMIC);
283                         if (newskb) {
284                                 dev_kfree_skb_irq(*skb);
285                                 *skb = newskb;
286                         } else
287                                 return -1;
288                         /* This behavior is not very subtile... */
289                 }
290
291                 /* make room for the new header and fill it. */
292                 avs =
293                     (struct avs_80211_1_header *) skb_push(*skb,
294                                                            sizeof (struct
295                                                                    avs_80211_1_header));
296                 
297                 avs->version = cpu_to_be32(P80211CAPTURE_VERSION);
298                 avs->length = cpu_to_be32(sizeof (struct avs_80211_1_header));
299                 avs->mactime = cpu_to_be64(le64_to_cpu(clock));
300                 avs->hosttime = cpu_to_be64(jiffies);
301                 avs->phytype = cpu_to_be32(6);  /*OFDM: 6 for (g), 8 for (a) */
302                 avs->channel = cpu_to_be32(channel_of_freq(freq));
303                 avs->datarate = cpu_to_be32(rate * 5);
304                 avs->antenna = cpu_to_be32(0);  /*unknown */
305                 avs->priority = cpu_to_be32(0); /*unknown */
306                 avs->ssi_type = cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
307                 avs->ssi_signal = cpu_to_be32(rssi & 0x7f);
308                 avs->ssi_noise = cpu_to_be32(priv->local_iwstatistics.qual.noise);      /*better than 'undefined', I assume */
309                 avs->preamble = cpu_to_be32(0); /*unknown */
310                 avs->encoding = cpu_to_be32(0); /*unknown */
311         } else
312                 skb_pull(*skb, sizeof (struct rfmon_header));
313
314         (*skb)->protocol = htons(ETH_P_802_2);
315         (*skb)->mac.raw = (*skb)->data;
316         (*skb)->pkt_type = PACKET_OTHERHOST;
317
318         return 0;
319 }
320
321 int
322 islpci_eth_receive(islpci_private *priv)
323 {
324         struct net_device *ndev = priv->ndev;
325         isl38xx_control_block *control_block = priv->control_block;
326         struct sk_buff *skb;
327         u16 size;
328         u32 index, offset;
329         unsigned char *src;
330         int discard = 0;
331
332 #if VERBOSE > SHOW_ERROR_MESSAGES
333         DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
334 #endif
335
336         /* the device has written an Ethernet frame in the data area
337          * of the sk_buff without updating the structure, do it now */
338         index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
339         size = le16_to_cpu(control_block->rx_data_low[index].size);
340         skb = priv->data_low_rx[index];
341         offset = ((unsigned long)
342                   le32_to_cpu(control_block->rx_data_low[index].address) -
343                   (unsigned long) skb->data) & 3;
344
345 #if VERBOSE > SHOW_ERROR_MESSAGES
346         DEBUG(SHOW_TRACING,
347               "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n ",
348               control_block->rx_data_low[priv->free_data_rx].address, skb->data,
349               skb->len, offset, skb->truesize);
350 #endif
351
352         /* delete the streaming DMA mapping before processing the skb */
353         pci_unmap_single(priv->pdev,
354                          priv->pci_map_rx_address[index],
355                          MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
356
357         /* update the skb structure and allign the buffer */
358         skb_put(skb, size);
359         if (offset) {
360                 /* shift the buffer allocation offset bytes to get the right frame */
361                 skb_pull(skb, 2);
362                 skb_put(skb, 2);
363         }
364 #if VERBOSE > SHOW_ERROR_MESSAGES
365         /* display the buffer contents for debugging */
366         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
367         display_buffer((char *) skb->data, skb->len);
368 #endif
369
370         /* check whether WDS is enabled and whether the data frame is a WDS frame */
371
372         if (init_wds) {
373                 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
374                 src = skb->data + 6;
375                 memmove(skb->data, src, skb->len - 6);
376                 skb_trim(skb, skb->len - 6);
377         }
378 #if VERBOSE > SHOW_ERROR_MESSAGES
379         DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
380         DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
381
382         /* display the buffer contents for debugging */
383         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
384         display_buffer((char *) skb->data, skb->len);
385 #endif
386
387         /* do some additional sk_buff and network layer parameters */
388         skb->dev = ndev;
389
390         /* take care of monitor mode and spy monitoring. */
391         if (unlikely(priv->iw_mode == IW_MODE_MONITOR))
392                 discard = islpci_monitor_rx(priv, &skb);
393         else {
394                 if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
395                         /* The packet has a rx_annex. Read it for spy monitoring, Then
396                          * remove it, while keeping the 2 leading MAC addr.
397                          */
398                         struct iw_quality wstats;
399                         struct rx_annex_header *annex =
400                             (struct rx_annex_header *) skb->data;
401                         wstats.level = annex->rfmon.rssi;
402                         /* The noise value can be a bit outdated if nobody's 
403                          * reading wireless stats... */
404                         wstats.noise = priv->local_iwstatistics.qual.noise;
405                         wstats.qual = wstats.level - wstats.noise;
406                         wstats.updated = 0x07;
407                         /* Update spy records */
408                         wireless_spy_update(ndev, annex->addr2, &wstats);
409
410                         memcpy(skb->data + sizeof (struct rfmon_header),
411                                skb->data, 2 * ETH_ALEN);
412                         skb_pull(skb, sizeof (struct rfmon_header));
413                 }
414                 skb->protocol = eth_type_trans(skb, ndev);
415         }
416         skb->ip_summed = CHECKSUM_NONE;
417         priv->statistics.rx_packets++;
418         priv->statistics.rx_bytes += size;
419
420         /* deliver the skb to the network layer */
421 #ifdef ISLPCI_ETH_DEBUG
422         printk
423             ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
424              skb->data[0], skb->data[1], skb->data[2], skb->data[3],
425              skb->data[4], skb->data[5]);
426 #endif
427         if (unlikely(discard)) {
428                 dev_kfree_skb_irq(skb);
429                 skb = NULL;
430         } else
431                 netif_rx(skb);
432
433         /* increment the read index for the rx data low queue */
434         priv->free_data_rx++;
435
436         /* add one or more sk_buff structures */
437         while (index =
438                le32_to_cpu(control_block->
439                            driver_curr_frag[ISL38XX_CB_RX_DATA_LQ]),
440                index - priv->free_data_rx < ISL38XX_CB_RX_QSIZE) {
441                 /* allocate an sk_buff for received data frames storage
442                  * include any required allignment operations */
443                 skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2);
444                 if (unlikely(skb == NULL)) {
445                         /* error allocating an sk_buff structure elements */
446                         DEBUG(SHOW_ERROR_MESSAGES, "Error allocating skb \n");
447                         break;
448                 }
449                 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
450                 /* store the new skb structure pointer */
451                 index = index % ISL38XX_CB_RX_QSIZE;
452                 priv->data_low_rx[index] = skb;
453
454 #if VERBOSE > SHOW_ERROR_MESSAGES
455                 DEBUG(SHOW_TRACING,
456                       "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n ",
457                       skb, skb->data, skb->len, index, skb->truesize);
458 #endif
459
460                 /* set the streaming DMA mapping for proper PCI bus operation */
461                 priv->pci_map_rx_address[index] =
462                     pci_map_single(priv->pdev, (void *) skb->data,
463                                    MAX_FRAGMENT_SIZE_RX + 2,
464                                    PCI_DMA_FROMDEVICE);
465                 if (unlikely(priv->pci_map_rx_address[index] == (dma_addr_t) NULL)) {
466                         /* error mapping the buffer to device accessable memory address */
467                         DEBUG(SHOW_ERROR_MESSAGES,
468                               "Error mapping DMA address\n");
469
470                         /* free the skbuf structure before aborting */
471                         dev_kfree_skb_irq((struct sk_buff *) skb);
472                         skb = NULL;
473                         break;
474                 }
475                 /* update the fragment address */
476                 control_block->rx_data_low[index].address = cpu_to_le32((u32)
477                                                                         priv->
478                                                                         pci_map_rx_address
479                                                                         [index]);
480                 wmb();
481
482                 /* increment the driver read pointer */
483                 add_le32p((u32 *) &control_block->
484                           driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
485         }
486
487         /* trigger the device */
488         islpci_trigger(priv);
489
490         return 0;
491 }
492
493 void
494 islpci_do_reset_and_wake(void *data)
495 {
496         islpci_private *priv = (islpci_private *) data;
497         islpci_reset(priv, 1);
498         netif_wake_queue(priv->ndev);
499         priv->reset_task_pending = 0;
500 }
501
502 void
503 islpci_eth_tx_timeout(struct net_device *ndev)
504 {
505         islpci_private *priv = netdev_priv(ndev);
506         struct net_device_stats *statistics = &priv->statistics;
507
508         /* increment the transmit error counter */
509         statistics->tx_errors++;
510
511         printk(KERN_WARNING "%s: tx_timeout", ndev->name);
512         if (!priv->reset_task_pending) {
513                 priv->reset_task_pending = 1;
514                 printk(", scheduling a reset");
515                 netif_stop_queue(ndev);
516                 schedule_work(&priv->reset_task);
517         }
518         printk("\n");
519 }