include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / 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 #include <linux/gfp.h>
21
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_arp.h>
27 #include <asm/byteorder.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]) {
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 netdev_tx_t
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
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                 goto drop_free;
111         }
112         /* Check alignment and WDS frame formatting. The start of the packet should
113          * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
114          * and add WDS address information */
115         if (likely(((long) skb->data & 0x03) | init_wds)) {
116                 /* get the number of bytes to add and re-allign */
117                 offset = (4 - (long) skb->data) & 0x03;
118                 offset += init_wds ? 6 : 0;
119
120                 /* check whether the current skb can be used  */
121                 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
122                         unsigned char *src = skb->data;
123
124 #if VERBOSE > SHOW_ERROR_MESSAGES
125                         DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
126                               init_wds);
127 #endif
128
129                         /* align the buffer on 4-byte boundary */
130                         skb_reserve(skb, (4 - (long) skb->data) & 0x03);
131                         if (init_wds) {
132                                 /* wds requires an additional address field of 6 bytes */
133                                 skb_put(skb, 6);
134 #ifdef ISLPCI_ETH_DEBUG
135                                 printk("islpci_eth_transmit:wds_mac\n");
136 #endif
137                                 memmove(skb->data + 6, src, skb->len);
138                                 skb_copy_to_linear_data(skb, wds_mac, 6);
139                         } else {
140                                 memmove(skb->data, src, skb->len);
141                         }
142
143 #if VERBOSE > SHOW_ERROR_MESSAGES
144                         DEBUG(SHOW_TRACING, "memmove %p %p %i \n", skb->data,
145                               src, skb->len);
146 #endif
147                 } else {
148                         newskb =
149                             dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
150                         if (unlikely(newskb == NULL)) {
151                                 printk(KERN_ERR "%s: Cannot allocate skb\n",
152                                        ndev->name);
153                                 goto drop_free;
154                         }
155                         newskb_offset = (4 - (long) newskb->data) & 0x03;
156
157                         /* Check if newskb->data is aligned */
158                         if (newskb_offset)
159                                 skb_reserve(newskb, newskb_offset);
160
161                         skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
162                         if (init_wds) {
163                                 skb_copy_from_linear_data(skb,
164                                                           newskb->data + 6,
165                                                           skb->len);
166                                 skb_copy_to_linear_data(newskb, wds_mac, 6);
167 #ifdef ISLPCI_ETH_DEBUG
168                                 printk("islpci_eth_transmit:wds_mac\n");
169 #endif
170                         } else
171                                 skb_copy_from_linear_data(skb, newskb->data,
172                                                           skb->len);
173
174 #if VERBOSE > SHOW_ERROR_MESSAGES
175                         DEBUG(SHOW_TRACING, "memcpy %p %p %i wds %i\n",
176                               newskb->data, skb->data, skb->len, init_wds);
177 #endif
178
179                         newskb->dev = skb->dev;
180                         dev_kfree_skb_irq(skb);
181                         skb = newskb;
182                 }
183         }
184         /* display the buffer contents for debugging */
185 #if VERBOSE > SHOW_ERROR_MESSAGES
186         DEBUG(SHOW_BUFFER_CONTENTS, "\ntx %p ", skb->data);
187         display_buffer((char *) skb->data, skb->len);
188 #endif
189
190         /* map the skb buffer to pci memory for DMA operation */
191         pci_map_address = pci_map_single(priv->pdev,
192                                          (void *) skb->data, skb->len,
193                                          PCI_DMA_TODEVICE);
194         if (unlikely(pci_map_address == 0)) {
195                 printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
196                        ndev->name);
197                 goto drop_free;
198         }
199         /* Place the fragment in the control block structure. */
200         index = curr_frag % ISL38XX_CB_TX_QSIZE;
201         fragment = &cb->tx_data_low[index];
202
203         priv->pci_map_tx_address[index] = pci_map_address;
204         /* store the skb address for future freeing  */
205         priv->data_low_tx[index] = skb;
206         /* set the proper fragment start address and size information */
207         frame_size = skb->len;
208         fragment->size = cpu_to_le16(frame_size);
209         fragment->flags = cpu_to_le16(0);       /* set to 1 if more fragments */
210         fragment->address = cpu_to_le32(pci_map_address);
211         curr_frag++;
212
213         /* The fragment address in the control block must have been
214          * written before announcing the frame buffer to device. */
215         wmb();
216         cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
217
218         if (curr_frag - priv->free_data_tx + ISL38XX_MIN_QTHRESHOLD
219             > ISL38XX_CB_TX_QSIZE) {
220                 /* stop sends from upper layers */
221                 netif_stop_queue(ndev);
222
223                 /* set the full flag for the transmission queue */
224                 priv->data_low_tx_full = 1;
225         }
226
227         /* set the transmission time */
228         ndev->trans_start = jiffies;
229         ndev->stats.tx_packets++;
230         ndev->stats.tx_bytes += skb->len;
231
232         /* trigger the device */
233         islpci_trigger(priv);
234
235         /* unlock the driver code */
236         spin_unlock_irqrestore(&priv->slock, flags);
237
238         return NETDEV_TX_OK;
239
240       drop_free:
241         ndev->stats.tx_dropped++;
242         spin_unlock_irqrestore(&priv->slock, flags);
243         dev_kfree_skb(skb);
244         return NETDEV_TX_OK;
245 }
246
247 static inline int
248 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
249 {
250         /* The card reports full 802.11 packets but with a 20 bytes
251          * header and without the FCS. But there a is a bit that
252          * indicates if the packet is corrupted :-) */
253         struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
254
255         if (hdr->flags & 0x01)
256                 /* This one is bad. Drop it ! */
257                 return -1;
258         if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
259                 struct avs_80211_1_header *avs;
260                 /* extract the relevant data from the header */
261                 u32 clock = le32_to_cpu(hdr->clock);
262                 u8 rate = hdr->rate;
263                 u16 freq = le16_to_cpu(hdr->freq);
264                 u8 rssi = hdr->rssi;
265
266                 skb_pull(*skb, sizeof (struct rfmon_header));
267
268                 if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
269                         struct sk_buff *newskb = skb_copy_expand(*skb,
270                                                                  sizeof (struct
271                                                                          avs_80211_1_header),
272                                                                  0, GFP_ATOMIC);
273                         if (newskb) {
274                                 dev_kfree_skb_irq(*skb);
275                                 *skb = newskb;
276                         } else
277                                 return -1;
278                         /* This behavior is not very subtile... */
279                 }
280
281                 /* make room for the new header and fill it. */
282                 avs =
283                     (struct avs_80211_1_header *) skb_push(*skb,
284                                                            sizeof (struct
285                                                                    avs_80211_1_header));
286
287                 avs->version = cpu_to_be32(P80211CAPTURE_VERSION);
288                 avs->length = cpu_to_be32(sizeof (struct avs_80211_1_header));
289                 avs->mactime = cpu_to_be64(clock);
290                 avs->hosttime = cpu_to_be64(jiffies);
291                 avs->phytype = cpu_to_be32(6);  /*OFDM: 6 for (g), 8 for (a) */
292                 avs->channel = cpu_to_be32(channel_of_freq(freq));
293                 avs->datarate = cpu_to_be32(rate * 5);
294                 avs->antenna = cpu_to_be32(0);  /*unknown */
295                 avs->priority = cpu_to_be32(0); /*unknown */
296                 avs->ssi_type = cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
297                 avs->ssi_signal = cpu_to_be32(rssi & 0x7f);
298                 avs->ssi_noise = cpu_to_be32(priv->local_iwstatistics.qual.noise);      /*better than 'undefined', I assume */
299                 avs->preamble = cpu_to_be32(0); /*unknown */
300                 avs->encoding = cpu_to_be32(0); /*unknown */
301         } else
302                 skb_pull(*skb, sizeof (struct rfmon_header));
303
304         (*skb)->protocol = htons(ETH_P_802_2);
305         skb_reset_mac_header(*skb);
306         (*skb)->pkt_type = PACKET_OTHERHOST;
307
308         return 0;
309 }
310
311 int
312 islpci_eth_receive(islpci_private *priv)
313 {
314         struct net_device *ndev = priv->ndev;
315         isl38xx_control_block *control_block = priv->control_block;
316         struct sk_buff *skb;
317         u16 size;
318         u32 index, offset;
319         unsigned char *src;
320         int discard = 0;
321
322 #if VERBOSE > SHOW_ERROR_MESSAGES
323         DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
324 #endif
325
326         /* the device has written an Ethernet frame in the data area
327          * of the sk_buff without updating the structure, do it now */
328         index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
329         size = le16_to_cpu(control_block->rx_data_low[index].size);
330         skb = priv->data_low_rx[index];
331         offset = ((unsigned long)
332                   le32_to_cpu(control_block->rx_data_low[index].address) -
333                   (unsigned long) skb->data) & 3;
334
335 #if VERBOSE > SHOW_ERROR_MESSAGES
336         DEBUG(SHOW_TRACING,
337               "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n ",
338               control_block->rx_data_low[priv->free_data_rx].address, skb->data,
339               skb->len, offset, skb->truesize);
340 #endif
341
342         /* delete the streaming DMA mapping before processing the skb */
343         pci_unmap_single(priv->pdev,
344                          priv->pci_map_rx_address[index],
345                          MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
346
347         /* update the skb structure and allign the buffer */
348         skb_put(skb, size);
349         if (offset) {
350                 /* shift the buffer allocation offset bytes to get the right frame */
351                 skb_pull(skb, 2);
352                 skb_put(skb, 2);
353         }
354 #if VERBOSE > SHOW_ERROR_MESSAGES
355         /* display the buffer contents for debugging */
356         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
357         display_buffer((char *) skb->data, skb->len);
358 #endif
359
360         /* check whether WDS is enabled and whether the data frame is a WDS frame */
361
362         if (init_wds) {
363                 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
364                 src = skb->data + 6;
365                 memmove(skb->data, src, skb->len - 6);
366                 skb_trim(skb, skb->len - 6);
367         }
368 #if VERBOSE > SHOW_ERROR_MESSAGES
369         DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
370         DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
371
372         /* display the buffer contents for debugging */
373         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
374         display_buffer((char *) skb->data, skb->len);
375 #endif
376         /* take care of monitor mode and spy monitoring. */
377         if (unlikely(priv->iw_mode == IW_MODE_MONITOR)) {
378                 skb->dev = ndev;
379                 discard = islpci_monitor_rx(priv, &skb);
380         } else {
381                 if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
382                         /* The packet has a rx_annex. Read it for spy monitoring, Then
383                          * remove it, while keeping the 2 leading MAC addr.
384                          */
385                         struct iw_quality wstats;
386                         struct rx_annex_header *annex =
387                             (struct rx_annex_header *) skb->data;
388                         wstats.level = annex->rfmon.rssi;
389                         /* The noise value can be a bit outdated if nobody's
390                          * reading wireless stats... */
391                         wstats.noise = priv->local_iwstatistics.qual.noise;
392                         wstats.qual = wstats.level - wstats.noise;
393                         wstats.updated = 0x07;
394                         /* Update spy records */
395                         wireless_spy_update(ndev, annex->addr2, &wstats);
396
397                         skb_copy_from_linear_data(skb,
398                                                   (skb->data +
399                                                    sizeof(struct rfmon_header)),
400                                                   2 * ETH_ALEN);
401                         skb_pull(skb, sizeof (struct rfmon_header));
402                 }
403                 skb->protocol = eth_type_trans(skb, ndev);
404         }
405         skb->ip_summed = CHECKSUM_NONE;
406         ndev->stats.rx_packets++;
407         ndev->stats.rx_bytes += size;
408
409         /* deliver the skb to the network layer */
410 #ifdef ISLPCI_ETH_DEBUG
411         printk
412             ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
413              skb->data[0], skb->data[1], skb->data[2], skb->data[3],
414              skb->data[4], skb->data[5]);
415 #endif
416         if (unlikely(discard)) {
417                 dev_kfree_skb_irq(skb);
418                 skb = NULL;
419         } else
420                 netif_rx(skb);
421
422         /* increment the read index for the rx data low queue */
423         priv->free_data_rx++;
424
425         /* add one or more sk_buff structures */
426         while (index =
427                le32_to_cpu(control_block->
428                            driver_curr_frag[ISL38XX_CB_RX_DATA_LQ]),
429                index - priv->free_data_rx < ISL38XX_CB_RX_QSIZE) {
430                 /* allocate an sk_buff for received data frames storage
431                  * include any required allignment operations */
432                 skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2);
433                 if (unlikely(skb == NULL)) {
434                         /* error allocating an sk_buff structure elements */
435                         DEBUG(SHOW_ERROR_MESSAGES, "Error allocating skb \n");
436                         break;
437                 }
438                 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
439                 /* store the new skb structure pointer */
440                 index = index % ISL38XX_CB_RX_QSIZE;
441                 priv->data_low_rx[index] = skb;
442
443 #if VERBOSE > SHOW_ERROR_MESSAGES
444                 DEBUG(SHOW_TRACING,
445                       "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n ",
446                       skb, skb->data, skb->len, index, skb->truesize);
447 #endif
448
449                 /* set the streaming DMA mapping for proper PCI bus operation */
450                 priv->pci_map_rx_address[index] =
451                     pci_map_single(priv->pdev, (void *) skb->data,
452                                    MAX_FRAGMENT_SIZE_RX + 2,
453                                    PCI_DMA_FROMDEVICE);
454                 if (unlikely(!priv->pci_map_rx_address[index])) {
455                         /* error mapping the buffer to device accessable memory address */
456                         DEBUG(SHOW_ERROR_MESSAGES,
457                               "Error mapping DMA address\n");
458
459                         /* free the skbuf structure before aborting */
460                         dev_kfree_skb_irq((struct sk_buff *) skb);
461                         skb = NULL;
462                         break;
463                 }
464                 /* update the fragment address */
465                 control_block->rx_data_low[index].address =
466                         cpu_to_le32((u32)priv->pci_map_rx_address[index]);
467                 wmb();
468
469                 /* increment the driver read pointer */
470                 le32_add_cpu(&control_block->
471                              driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
472         }
473
474         /* trigger the device */
475         islpci_trigger(priv);
476
477         return 0;
478 }
479
480 void
481 islpci_do_reset_and_wake(struct work_struct *work)
482 {
483         islpci_private *priv = container_of(work, islpci_private, reset_task);
484
485         islpci_reset(priv, 1);
486         priv->reset_task_pending = 0;
487         smp_wmb();
488         netif_wake_queue(priv->ndev);
489 }
490
491 void
492 islpci_eth_tx_timeout(struct net_device *ndev)
493 {
494         islpci_private *priv = netdev_priv(ndev);
495
496         /* increment the transmit error counter */
497         ndev->stats.tx_errors++;
498
499         if (!priv->reset_task_pending) {
500                 printk(KERN_WARNING
501                         "%s: tx_timeout, scheduling reset", ndev->name);
502                 netif_stop_queue(ndev);
503                 priv->reset_task_pending = 1;
504                 schedule_work(&priv->reset_task);
505         } else {
506                 printk(KERN_WARNING
507                         "%s: tx_timeout, waiting for reset", ndev->name);
508         }
509 }