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