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
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include <linux/module.h>
19 #include <linux/gfp.h>
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>
28 #include "prismcompat.h"
30 #include "islpci_eth.h"
31 #include "islpci_mgt.h"
34 /******************************************************************************
35 Network Interface functions
36 ******************************************************************************/
38 islpci_eth_cleanup_transmit(islpci_private
*priv
,
39 isl38xx_control_block
*control_block
)
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
;
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
]) {
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
];
58 #if VERBOSE > SHOW_ERROR_MESSAGES
60 "cleanup skb %p skb->data %p skb->len %u truesize %u\n",
61 skb
, skb
->data
, skb
->len
, skb
->truesize
);
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
);
70 /* increment the free data low queue pointer */
76 islpci_eth_transmit(struct sk_buff
*skb
, struct net_device
*ndev
)
78 islpci_private
*priv
= netdev_priv(ndev
);
79 isl38xx_control_block
*cb
= priv
->control_block
;
81 dma_addr_t pci_map_address
;
83 isl38xx_fragment
*fragment
;
85 struct sk_buff
*newskb
;
88 unsigned char wds_mac
[6];
91 #if VERBOSE > SHOW_ERROR_MESSAGES
92 DEBUG(SHOW_FUNCTION_CALLS
, "islpci_eth_transmit\n");
95 /* lock the driver code */
96 spin_lock_irqsave(&priv
->slock
, flags
);
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",
103 netif_stop_queue(ndev
);
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
);
111 /* Check alignment and WDS frame formatting. The start of the packet should
112 * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
113 * and add WDS address information */
114 if (likely(((long) skb
->data
& 0x03) | init_wds
)) {
115 /* get the number of bytes to add and re-align */
116 offset
= (4 - (long) skb
->data
) & 0x03;
117 offset
+= init_wds
? 6 : 0;
119 /* check whether the current skb can be used */
120 if (!skb_cloned(skb
) && (skb_tailroom(skb
) >= offset
)) {
121 unsigned char *src
= skb
->data
;
123 #if VERBOSE > SHOW_ERROR_MESSAGES
124 DEBUG(SHOW_TRACING
, "skb offset %i wds %i\n", offset
,
128 /* align the buffer on 4-byte boundary */
129 skb_reserve(skb
, (4 - (long) skb
->data
) & 0x03);
131 /* wds requires an additional address field of 6 bytes */
133 #ifdef ISLPCI_ETH_DEBUG
134 printk("islpci_eth_transmit:wds_mac\n");
136 memmove(skb
->data
+ 6, src
, skb
->len
);
137 skb_copy_to_linear_data(skb
, wds_mac
, 6);
139 memmove(skb
->data
, src
, skb
->len
);
142 #if VERBOSE > SHOW_ERROR_MESSAGES
143 DEBUG(SHOW_TRACING
, "memmove %p %p %i\n", skb
->data
,
148 dev_alloc_skb(init_wds
? skb
->len
+ 6 : skb
->len
);
149 if (unlikely(newskb
== NULL
)) {
150 printk(KERN_ERR
"%s: Cannot allocate skb\n",
154 newskb_offset
= (4 - (long) newskb
->data
) & 0x03;
156 /* Check if newskb->data is aligned */
158 skb_reserve(newskb
, newskb_offset
);
160 skb_put(newskb
, init_wds
? skb
->len
+ 6 : skb
->len
);
162 skb_copy_from_linear_data(skb
,
165 skb_copy_to_linear_data(newskb
, wds_mac
, 6);
166 #ifdef ISLPCI_ETH_DEBUG
167 printk("islpci_eth_transmit:wds_mac\n");
170 skb_copy_from_linear_data(skb
, newskb
->data
,
173 #if VERBOSE > SHOW_ERROR_MESSAGES
174 DEBUG(SHOW_TRACING
, "memcpy %p %p %i wds %i\n",
175 newskb
->data
, skb
->data
, skb
->len
, init_wds
);
178 newskb
->dev
= skb
->dev
;
179 dev_kfree_skb_irq(skb
);
183 /* display the buffer contents for debugging */
184 #if VERBOSE > SHOW_ERROR_MESSAGES
185 DEBUG(SHOW_BUFFER_CONTENTS
, "\ntx %p ", skb
->data
);
186 display_buffer((char *) skb
->data
, skb
->len
);
189 /* map the skb buffer to pci memory for DMA operation */
190 pci_map_address
= pci_map_single(priv
->pdev
,
191 (void *) skb
->data
, skb
->len
,
193 if (pci_dma_mapping_error(priv
->pdev
, pci_map_address
)) {
194 printk(KERN_WARNING
"%s: cannot map buffer to PCI\n",
198 /* Place the fragment in the control block structure. */
199 index
= curr_frag
% ISL38XX_CB_TX_QSIZE
;
200 fragment
= &cb
->tx_data_low
[index
];
202 priv
->pci_map_tx_address
[index
] = pci_map_address
;
203 /* store the skb address for future freeing */
204 priv
->data_low_tx
[index
] = skb
;
205 /* set the proper fragment start address and size information */
206 frame_size
= skb
->len
;
207 fragment
->size
= cpu_to_le16(frame_size
);
208 fragment
->flags
= cpu_to_le16(0); /* set to 1 if more fragments */
209 fragment
->address
= cpu_to_le32(pci_map_address
);
212 /* The fragment address in the control block must have been
213 * written before announcing the frame buffer to device. */
215 cb
->driver_curr_frag
[ISL38XX_CB_TX_DATA_LQ
] = cpu_to_le32(curr_frag
);
217 if (curr_frag
- priv
->free_data_tx
+ ISL38XX_MIN_QTHRESHOLD
218 > ISL38XX_CB_TX_QSIZE
) {
219 /* stop sends from upper layers */
220 netif_stop_queue(ndev
);
222 /* set the full flag for the transmission queue */
223 priv
->data_low_tx_full
= 1;
226 ndev
->stats
.tx_packets
++;
227 ndev
->stats
.tx_bytes
+= skb
->len
;
229 /* trigger the device */
230 islpci_trigger(priv
);
232 /* unlock the driver code */
233 spin_unlock_irqrestore(&priv
->slock
, flags
);
238 ndev
->stats
.tx_dropped
++;
239 spin_unlock_irqrestore(&priv
->slock
, flags
);
245 islpci_monitor_rx(islpci_private
*priv
, struct sk_buff
**skb
)
247 /* The card reports full 802.11 packets but with a 20 bytes
248 * header and without the FCS. But there a is a bit that
249 * indicates if the packet is corrupted :-) */
250 struct rfmon_header
*hdr
= (struct rfmon_header
*) (*skb
)->data
;
252 if (hdr
->flags
& 0x01)
253 /* This one is bad. Drop it ! */
255 if (priv
->ndev
->type
== ARPHRD_IEEE80211_PRISM
) {
256 struct avs_80211_1_header
*avs
;
257 /* extract the relevant data from the header */
258 u32 clock
= le32_to_cpu(hdr
->clock
);
260 u16 freq
= le16_to_cpu(hdr
->freq
);
263 skb_pull(*skb
, sizeof (struct rfmon_header
));
265 if (skb_headroom(*skb
) < sizeof (struct avs_80211_1_header
)) {
266 struct sk_buff
*newskb
= skb_copy_expand(*skb
,
271 dev_kfree_skb_irq(*skb
);
275 /* This behavior is not very subtile... */
278 /* make room for the new header and fill it. */
279 avs
= skb_push(*skb
, sizeof(struct avs_80211_1_header
));
281 avs
->version
= cpu_to_be32(P80211CAPTURE_VERSION
);
282 avs
->length
= cpu_to_be32(sizeof (struct avs_80211_1_header
));
283 avs
->mactime
= cpu_to_be64(clock
);
284 avs
->hosttime
= cpu_to_be64(jiffies
);
285 avs
->phytype
= cpu_to_be32(6); /*OFDM: 6 for (g), 8 for (a) */
286 avs
->channel
= cpu_to_be32(channel_of_freq(freq
));
287 avs
->datarate
= cpu_to_be32(rate
* 5);
288 avs
->antenna
= cpu_to_be32(0); /*unknown */
289 avs
->priority
= cpu_to_be32(0); /*unknown */
290 avs
->ssi_type
= cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
291 avs
->ssi_signal
= cpu_to_be32(rssi
& 0x7f);
292 avs
->ssi_noise
= cpu_to_be32(priv
->local_iwstatistics
.qual
.noise
); /*better than 'undefined', I assume */
293 avs
->preamble
= cpu_to_be32(0); /*unknown */
294 avs
->encoding
= cpu_to_be32(0); /*unknown */
296 skb_pull(*skb
, sizeof (struct rfmon_header
));
298 (*skb
)->protocol
= htons(ETH_P_802_2
);
299 skb_reset_mac_header(*skb
);
300 (*skb
)->pkt_type
= PACKET_OTHERHOST
;
306 islpci_eth_receive(islpci_private
*priv
)
308 struct net_device
*ndev
= priv
->ndev
;
309 isl38xx_control_block
*control_block
= priv
->control_block
;
316 #if VERBOSE > SHOW_ERROR_MESSAGES
317 DEBUG(SHOW_FUNCTION_CALLS
, "islpci_eth_receive\n");
320 /* the device has written an Ethernet frame in the data area
321 * of the sk_buff without updating the structure, do it now */
322 index
= priv
->free_data_rx
% ISL38XX_CB_RX_QSIZE
;
323 size
= le16_to_cpu(control_block
->rx_data_low
[index
].size
);
324 skb
= priv
->data_low_rx
[index
];
325 offset
= ((unsigned long)
326 le32_to_cpu(control_block
->rx_data_low
[index
].address
) -
327 (unsigned long) skb
->data
) & 3;
329 #if VERBOSE > SHOW_ERROR_MESSAGES
331 "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n",
332 control_block
->rx_data_low
[priv
->free_data_rx
].address
, skb
->data
,
333 skb
->len
, offset
, skb
->truesize
);
336 /* delete the streaming DMA mapping before processing the skb */
337 pci_unmap_single(priv
->pdev
,
338 priv
->pci_map_rx_address
[index
],
339 MAX_FRAGMENT_SIZE_RX
+ 2, PCI_DMA_FROMDEVICE
);
341 /* update the skb structure and align the buffer */
344 /* shift the buffer allocation offset bytes to get the right frame */
348 #if VERBOSE > SHOW_ERROR_MESSAGES
349 /* display the buffer contents for debugging */
350 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
351 display_buffer((char *) skb
->data
, skb
->len
);
354 /* check whether WDS is enabled and whether the data frame is a WDS frame */
357 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
359 memmove(skb
->data
, src
, skb
->len
- 6);
360 skb_trim(skb
, skb
->len
- 6);
362 #if VERBOSE > SHOW_ERROR_MESSAGES
363 DEBUG(SHOW_TRACING
, "Fragment size %i in skb at %p\n", size
, skb
);
364 DEBUG(SHOW_TRACING
, "Skb data at %p, length %i\n", skb
->data
, skb
->len
);
366 /* display the buffer contents for debugging */
367 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
368 display_buffer((char *) skb
->data
, skb
->len
);
370 /* take care of monitor mode and spy monitoring. */
371 if (unlikely(priv
->iw_mode
== IW_MODE_MONITOR
)) {
373 discard
= islpci_monitor_rx(priv
, &skb
);
375 if (unlikely(skb
->data
[2 * ETH_ALEN
] == 0)) {
376 /* The packet has a rx_annex. Read it for spy monitoring, Then
377 * remove it, while keeping the 2 leading MAC addr.
379 struct iw_quality wstats
;
380 struct rx_annex_header
*annex
=
381 (struct rx_annex_header
*) skb
->data
;
382 wstats
.level
= annex
->rfmon
.rssi
;
383 /* The noise value can be a bit outdated if nobody's
384 * reading wireless stats... */
385 wstats
.noise
= priv
->local_iwstatistics
.qual
.noise
;
386 wstats
.qual
= wstats
.level
- wstats
.noise
;
387 wstats
.updated
= 0x07;
388 /* Update spy records */
389 wireless_spy_update(ndev
, annex
->addr2
, &wstats
);
391 skb_copy_from_linear_data(skb
,
393 sizeof(struct rfmon_header
)),
395 skb_pull(skb
, sizeof (struct rfmon_header
));
397 skb
->protocol
= eth_type_trans(skb
, ndev
);
399 skb
->ip_summed
= CHECKSUM_NONE
;
400 ndev
->stats
.rx_packets
++;
401 ndev
->stats
.rx_bytes
+= size
;
403 /* deliver the skb to the network layer */
404 #ifdef ISLPCI_ETH_DEBUG
406 ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
407 skb
->data
[0], skb
->data
[1], skb
->data
[2], skb
->data
[3],
408 skb
->data
[4], skb
->data
[5]);
410 if (unlikely(discard
)) {
411 dev_kfree_skb_irq(skb
);
416 /* increment the read index for the rx data low queue */
417 priv
->free_data_rx
++;
419 /* add one or more sk_buff structures */
421 le32_to_cpu(control_block
->
422 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
]),
423 index
- priv
->free_data_rx
< ISL38XX_CB_RX_QSIZE
) {
424 /* allocate an sk_buff for received data frames storage
425 * include any required allignment operations */
426 skb
= dev_alloc_skb(MAX_FRAGMENT_SIZE_RX
+ 2);
427 if (unlikely(skb
== NULL
)) {
428 /* error allocating an sk_buff structure elements */
429 DEBUG(SHOW_ERROR_MESSAGES
, "Error allocating skb\n");
432 skb_reserve(skb
, (4 - (long) skb
->data
) & 0x03);
433 /* store the new skb structure pointer */
434 index
= index
% ISL38XX_CB_RX_QSIZE
;
435 priv
->data_low_rx
[index
] = skb
;
437 #if VERBOSE > SHOW_ERROR_MESSAGES
439 "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n",
440 skb
, skb
->data
, skb
->len
, index
, skb
->truesize
);
443 /* set the streaming DMA mapping for proper PCI bus operation */
444 priv
->pci_map_rx_address
[index
] =
445 pci_map_single(priv
->pdev
, (void *) skb
->data
,
446 MAX_FRAGMENT_SIZE_RX
+ 2,
448 if (pci_dma_mapping_error(priv
->pdev
,
449 priv
->pci_map_rx_address
[index
])) {
450 /* error mapping the buffer to device accessible memory address */
451 DEBUG(SHOW_ERROR_MESSAGES
,
452 "Error mapping DMA address\n");
454 /* free the skbuf structure before aborting */
455 dev_kfree_skb_irq(skb
);
459 /* update the fragment address */
460 control_block
->rx_data_low
[index
].address
=
461 cpu_to_le32((u32
)priv
->pci_map_rx_address
[index
]);
464 /* increment the driver read pointer */
465 le32_add_cpu(&control_block
->
466 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
], 1);
469 /* trigger the device */
470 islpci_trigger(priv
);
476 islpci_do_reset_and_wake(struct work_struct
*work
)
478 islpci_private
*priv
= container_of(work
, islpci_private
, reset_task
);
480 islpci_reset(priv
, 1);
481 priv
->reset_task_pending
= 0;
483 netif_wake_queue(priv
->ndev
);
487 islpci_eth_tx_timeout(struct net_device
*ndev
)
489 islpci_private
*priv
= netdev_priv(ndev
);
491 /* increment the transmit error counter */
492 ndev
->stats
.tx_errors
++;
494 if (!priv
->reset_task_pending
) {
496 "%s: tx_timeout, scheduling reset", ndev
->name
);
497 netif_stop_queue(ndev
);
498 priv
->reset_task_pending
= 1;
499 schedule_work(&priv
->reset_task
);
502 "%s: tx_timeout, waiting for reset", ndev
->name
);