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