Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[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
27 #include "prismcompat.h"
28 #include "isl_38xx.h"
29 #include "islpci_eth.h"
30 #include "islpci_mgt.h"
31 #include "oid_mgt.h"
32
33 /******************************************************************************
34     Network Interface functions
35 ******************************************************************************/
36 void
37 islpci_eth_cleanup_transmit(islpci_private *priv,
38                             isl38xx_control_block *control_block)
39 {
40         struct sk_buff *skb;
41         u32 index;
42
43         /* compare the control block read pointer with the free pointer */
44         while (priv->free_data_tx !=
45                le32_to_cpu(control_block->
46                            device_curr_frag[ISL38XX_CB_TX_DATA_LQ])) {
47                 /* read the index of the first fragment to be freed */
48                 index = priv->free_data_tx % ISL38XX_CB_TX_QSIZE;
49
50                 /* check for holes in the arrays caused by multi fragment frames
51                  * searching for the last fragment of a frame */
52                 if (priv->pci_map_tx_address[index] != (dma_addr_t) NULL) {
53                         /* entry is the last fragment of a frame
54                          * free the skb structure and unmap pci memory */
55                         skb = priv->data_low_tx[index];
56
57 #if VERBOSE > SHOW_ERROR_MESSAGES
58                         DEBUG(SHOW_TRACING,
59                               "cleanup skb %p skb->data %p skb->len %u truesize %u\n ",
60                               skb, skb->data, skb->len, skb->truesize);
61 #endif
62
63                         pci_unmap_single(priv->pdev,
64                                          priv->pci_map_tx_address[index],
65                                          skb->len, PCI_DMA_TODEVICE);
66                         dev_kfree_skb_irq(skb);
67                         skb = NULL;
68                 }
69                 /* increment the free data low queue pointer */
70                 priv->free_data_tx++;
71         }
72 }
73
74 int
75 islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
76 {
77         islpci_private *priv = netdev_priv(ndev);
78         isl38xx_control_block *cb = priv->control_block;
79         u32 index;
80         dma_addr_t pci_map_address;
81         int frame_size;
82         isl38xx_fragment *fragment;
83         int offset;
84         struct sk_buff *newskb;
85         int newskb_offset;
86         unsigned long flags;
87         unsigned char wds_mac[6];
88         u32 curr_frag;
89         int err = 0;
90
91 #if VERBOSE > SHOW_ERROR_MESSAGES
92         DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit \n");
93 #endif
94
95         /* lock the driver code */
96         spin_lock_irqsave(&priv->slock, flags);
97
98         /* check whether the destination queue has enough fragments for the frame */
99         curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ]);
100         if (unlikely(curr_frag - priv->free_data_tx >= ISL38XX_CB_TX_QSIZE)) {
101                 printk(KERN_ERR "%s: transmit device queue full when awake\n",
102                        ndev->name);
103                 netif_stop_queue(ndev);
104
105                 /* trigger the device */
106                 isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
107                                   ISL38XX_DEV_INT_REG);
108                 udelay(ISL38XX_WRITEIO_DELAY);
109
110                 err = -EBUSY;
111                 goto drop_free;
112         }
113         /* Check alignment and WDS frame formatting. The start of the packet should
114          * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
115          * and add WDS address information */
116         if (likely(((long) skb->data & 0x03) | init_wds)) {
117                 /* get the number of bytes to add and re-allign */
118                 offset = (4 - (long) skb->data) & 0x03;
119                 offset += init_wds ? 6 : 0;
120
121                 /* check whether the current skb can be used  */
122                 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
123                         unsigned char *src = skb->data;
124
125 #if VERBOSE > SHOW_ERROR_MESSAGES
126                         DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
127                               init_wds);
128 #endif
129
130                         /* align the buffer on 4-byte boundary */
131                         skb_reserve(skb, (4 - (long) skb->data) & 0x03);
132                         if (init_wds) {
133                                 /* wds requires an additional address field of 6 bytes */
134                                 skb_put(skb, 6);
135 #ifdef ISLPCI_ETH_DEBUG
136                                 printk("islpci_eth_transmit:wds_mac\n");
137 #endif
138                                 memmove(skb->data + 6, src, skb->len);
139                                 skb_copy_to_linear_data(skb, wds_mac, 6);
140                         } else {
141                                 memmove(skb->data, src, skb->len);
142                         }
143
144 #if VERBOSE > SHOW_ERROR_MESSAGES
145                         DEBUG(SHOW_TRACING, "memmove %p %p %i \n", skb->data,
146                               src, skb->len);
147 #endif
148                 } else {
149                         newskb =
150                             dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
151                         if (unlikely(newskb == NULL)) {
152                                 printk(KERN_ERR "%s: Cannot allocate skb\n",
153                                        ndev->name);
154                                 err = -ENOMEM;
155                                 goto drop_free;
156                         }
157                         newskb_offset = (4 - (long) newskb->data) & 0x03;
158
159                         /* Check if newskb->data is aligned */
160                         if (newskb_offset)
161                                 skb_reserve(newskb, newskb_offset);
162
163                         skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
164                         if (init_wds) {
165                                 skb_copy_from_linear_data(skb,
166                                                           newskb->data + 6,
167                                                           skb->len);
168                                 skb_copy_to_linear_data(newskb, wds_mac, 6);
169 #ifdef ISLPCI_ETH_DEBUG
170                                 printk("islpci_eth_transmit:wds_mac\n");
171 #endif
172                         } else
173                                 skb_copy_from_linear_data(skb, newskb->data,
174                                                           skb->len);
175
176 #if VERBOSE > SHOW_ERROR_MESSAGES
177                         DEBUG(SHOW_TRACING, "memcpy %p %p %i wds %i\n",
178                               newskb->data, skb->data, skb->len, init_wds);
179 #endif
180
181                         newskb->dev = skb->dev;
182                         dev_kfree_skb_irq(skb);
183                         skb = newskb;
184                 }
185         }
186         /* display the buffer contents for debugging */
187 #if VERBOSE > SHOW_ERROR_MESSAGES
188         DEBUG(SHOW_BUFFER_CONTENTS, "\ntx %p ", skb->data);
189         display_buffer((char *) skb->data, skb->len);
190 #endif
191
192         /* map the skb buffer to pci memory for DMA operation */
193         pci_map_address = pci_map_single(priv->pdev,
194                                          (void *) skb->data, skb->len,
195                                          PCI_DMA_TODEVICE);
196         if (unlikely(pci_map_address == 0)) {
197                 printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
198                        ndev->name);
199
200                 err = -EIO;
201                 goto drop_free;
202         }
203         /* Place the fragment in the control block structure. */
204         index = curr_frag % ISL38XX_CB_TX_QSIZE;
205         fragment = &cb->tx_data_low[index];
206
207         priv->pci_map_tx_address[index] = pci_map_address;
208         /* store the skb address for future freeing  */
209         priv->data_low_tx[index] = skb;
210         /* set the proper fragment start address and size information */
211         frame_size = skb->len;
212         fragment->size = cpu_to_le16(frame_size);
213         fragment->flags = cpu_to_le16(0);       /* set to 1 if more fragments */
214         fragment->address = cpu_to_le32(pci_map_address);
215         curr_frag++;
216
217         /* The fragment address in the control block must have been
218          * written before announcing the frame buffer to device. */
219         wmb();
220         cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
221
222         if (curr_frag - priv->free_data_tx + ISL38XX_MIN_QTHRESHOLD
223             > ISL38XX_CB_TX_QSIZE) {
224                 /* stop sends from upper layers */
225                 netif_stop_queue(ndev);
226
227                 /* set the full flag for the transmission queue */
228                 priv->data_low_tx_full = 1;
229         }
230
231         /* set the transmission time */
232         ndev->trans_start = jiffies;
233         priv->statistics.tx_packets++;
234         priv->statistics.tx_bytes += skb->len;
235
236         /* trigger the device */
237         islpci_trigger(priv);
238
239         /* unlock the driver code */
240         spin_unlock_irqrestore(&priv->slock, flags);
241
242         return 0;
243
244       drop_free:
245         priv->statistics.tx_dropped++;
246         spin_unlock_irqrestore(&priv->slock, flags);
247         dev_kfree_skb(skb);
248         return err;
249 }
250
251 static inline int
252 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
253 {
254         /* The card reports full 802.11 packets but with a 20 bytes
255          * header and without the FCS. But there a is a bit that
256          * indicates if the packet is corrupted :-) */
257         struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
258
259         if (hdr->flags & 0x01)
260                 /* This one is bad. Drop it ! */
261                 return -1;
262         if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
263                 struct avs_80211_1_header *avs;
264                 /* extract the relevant data from the header */
265                 u32 clock = le32_to_cpu(hdr->clock);
266                 u8 rate = hdr->rate;
267                 u16 freq = le16_to_cpu(hdr->freq);
268                 u8 rssi = hdr->rssi;
269
270                 skb_pull(*skb, sizeof (struct rfmon_header));
271
272                 if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
273                         struct sk_buff *newskb = skb_copy_expand(*skb,
274                                                                  sizeof (struct
275                                                                          avs_80211_1_header),
276                                                                  0, GFP_ATOMIC);
277                         if (newskb) {
278                                 dev_kfree_skb_irq(*skb);
279                                 *skb = newskb;
280                         } else
281                                 return -1;
282                         /* This behavior is not very subtile... */
283                 }
284
285                 /* make room for the new header and fill it. */
286                 avs =
287                     (struct avs_80211_1_header *) skb_push(*skb,
288                                                            sizeof (struct
289                                                                    avs_80211_1_header));
290
291                 avs->version = cpu_to_be32(P80211CAPTURE_VERSION);
292                 avs->length = cpu_to_be32(sizeof (struct avs_80211_1_header));
293                 avs->mactime = cpu_to_be64(le64_to_cpu(clock));
294                 avs->hosttime = cpu_to_be64(jiffies);
295                 avs->phytype = cpu_to_be32(6);  /*OFDM: 6 for (g), 8 for (a) */
296                 avs->channel = cpu_to_be32(channel_of_freq(freq));
297                 avs->datarate = cpu_to_be32(rate * 5);
298                 avs->antenna = cpu_to_be32(0);  /*unknown */
299                 avs->priority = cpu_to_be32(0); /*unknown */
300                 avs->ssi_type = cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
301                 avs->ssi_signal = cpu_to_be32(rssi & 0x7f);
302                 avs->ssi_noise = cpu_to_be32(priv->local_iwstatistics.qual.noise);      /*better than 'undefined', I assume */
303                 avs->preamble = cpu_to_be32(0); /*unknown */
304                 avs->encoding = cpu_to_be32(0); /*unknown */
305         } else
306                 skb_pull(*skb, sizeof (struct rfmon_header));
307
308         (*skb)->protocol = htons(ETH_P_802_2);
309         skb_reset_mac_header(*skb);
310         (*skb)->pkt_type = PACKET_OTHERHOST;
311
312         return 0;
313 }
314
315 int
316 islpci_eth_receive(islpci_private *priv)
317 {
318         struct net_device *ndev = priv->ndev;
319         isl38xx_control_block *control_block = priv->control_block;
320         struct sk_buff *skb;
321         u16 size;
322         u32 index, offset;
323         unsigned char *src;
324         int discard = 0;
325
326 #if VERBOSE > SHOW_ERROR_MESSAGES
327         DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
328 #endif
329
330         /* the device has written an Ethernet frame in the data area
331          * of the sk_buff without updating the structure, do it now */
332         index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
333         size = le16_to_cpu(control_block->rx_data_low[index].size);
334         skb = priv->data_low_rx[index];
335         offset = ((unsigned long)
336                   le32_to_cpu(control_block->rx_data_low[index].address) -
337                   (unsigned long) skb->data) & 3;
338
339 #if VERBOSE > SHOW_ERROR_MESSAGES
340         DEBUG(SHOW_TRACING,
341               "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n ",
342               control_block->rx_data_low[priv->free_data_rx].address, skb->data,
343               skb->len, offset, skb->truesize);
344 #endif
345
346         /* delete the streaming DMA mapping before processing the skb */
347         pci_unmap_single(priv->pdev,
348                          priv->pci_map_rx_address[index],
349                          MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
350
351         /* update the skb structure and allign the buffer */
352         skb_put(skb, size);
353         if (offset) {
354                 /* shift the buffer allocation offset bytes to get the right frame */
355                 skb_pull(skb, 2);
356                 skb_put(skb, 2);
357         }
358 #if VERBOSE > SHOW_ERROR_MESSAGES
359         /* display the buffer contents for debugging */
360         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
361         display_buffer((char *) skb->data, skb->len);
362 #endif
363
364         /* check whether WDS is enabled and whether the data frame is a WDS frame */
365
366         if (init_wds) {
367                 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
368                 src = skb->data + 6;
369                 memmove(skb->data, src, skb->len - 6);
370                 skb_trim(skb, skb->len - 6);
371         }
372 #if VERBOSE > SHOW_ERROR_MESSAGES
373         DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
374         DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
375
376         /* display the buffer contents for debugging */
377         DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
378         display_buffer((char *) skb->data, skb->len);
379 #endif
380         /* take care of monitor mode and spy monitoring. */
381         if (unlikely(priv->iw_mode == IW_MODE_MONITOR))
382                 discard = islpci_monitor_rx(priv, &skb);
383         else {
384                 if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
385                         /* The packet has a rx_annex. Read it for spy monitoring, Then
386                          * remove it, while keeping the 2 leading MAC addr.
387                          */
388                         struct iw_quality wstats;
389                         struct rx_annex_header *annex =
390                             (struct rx_annex_header *) skb->data;
391                         wstats.level = annex->rfmon.rssi;
392                         /* The noise value can be a bit outdated if nobody's
393                          * reading wireless stats... */
394                         wstats.noise = priv->local_iwstatistics.qual.noise;
395                         wstats.qual = wstats.level - wstats.noise;
396                         wstats.updated = 0x07;
397                         /* Update spy records */
398                         wireless_spy_update(ndev, annex->addr2, &wstats);
399
400                         skb_copy_from_linear_data(skb,
401                                                   (skb->data +
402                                                    sizeof(struct rfmon_header)),
403                                                   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 =
469                         cpu_to_le32((u32)priv->pci_map_rx_address[index]);
470                 wmb();
471
472                 /* increment the driver read pointer */
473                 add_le32p((u32 *) &control_block->
474                           driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
475         }
476
477         /* trigger the device */
478         islpci_trigger(priv);
479
480         return 0;
481 }
482
483 void
484 islpci_do_reset_and_wake(struct work_struct *work)
485 {
486         islpci_private *priv = container_of(work, islpci_private, reset_task);
487
488         islpci_reset(priv, 1);
489         priv->reset_task_pending = 0;
490         smp_wmb();
491         netif_wake_queue(priv->ndev);
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         if (!priv->reset_task_pending) {
504                 printk(KERN_WARNING
505                         "%s: tx_timeout, scheduling reset", ndev->name);
506                 netif_stop_queue(ndev);
507                 priv->reset_task_pending = 1;
508                 schedule_work(&priv->reset_task);
509         } else {
510                 printk(KERN_WARNING
511                         "%s: tx_timeout, waiting for reset", ndev->name);
512         }
513 }