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