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
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.
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
20 #include <linux/version.h>
21 #include <linux/module.h>
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>
29 #include "prismcompat.h"
31 #include "islpci_eth.h"
32 #include "islpci_mgt.h"
35 /******************************************************************************
36 Network Interface functions
37 ******************************************************************************/
39 islpci_eth_cleanup_transmit(islpci_private *priv,
40 isl38xx_control_block *control_block)
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;
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];
59 #if VERBOSE > SHOW_ERROR_MESSAGES
61 "cleanup skb %p skb->data %p skb->len %u truesize %u\n ",
62 skb, skb->data, skb->len, skb->truesize);
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);
71 /* increment the free data low queue pointer */
77 islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
79 islpci_private *priv = netdev_priv(ndev);
80 isl38xx_control_block *cb = priv->control_block;
82 dma_addr_t pci_map_address;
84 isl38xx_fragment *fragment;
86 struct sk_buff *newskb;
89 unsigned char wds_mac[6];
93 #if VERBOSE > SHOW_ERROR_MESSAGES
94 DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit \n");
97 /* lock the driver code */
98 spin_lock_irqsave(&priv->slock, flags);
100 /* determine the amount of fragments needed to store the frame */
102 frame_size = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
106 /* check whether the destination queue has enough fragments for the frame */
107 curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ]);
108 if (unlikely(curr_frag - priv->free_data_tx >= ISL38XX_CB_TX_QSIZE)) {
109 printk(KERN_ERR "%s: transmit device queue full when awake\n",
111 netif_stop_queue(ndev);
113 /* trigger the device */
114 isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
115 ISL38XX_DEV_INT_REG);
116 udelay(ISL38XX_WRITEIO_DELAY);
121 /* Check alignment and WDS frame formatting. The start of the packet should
122 * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
123 * and add WDS address information */
124 if (unlikely(((long) skb->data & 0x03) | init_wds)) {
125 /* get the number of bytes to add and re-allign */
126 offset = (4 - (long) skb->data) & 0x03;
127 offset += init_wds ? 6 : 0;
129 /* check whether the current skb can be used */
130 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
131 unsigned char *src = skb->data;
133 #if VERBOSE > SHOW_ERROR_MESSAGES
134 DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
138 /* align the buffer on 4-byte boundary */
139 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
141 /* wds requires an additional address field of 6 bytes */
143 #ifdef ISLPCI_ETH_DEBUG
144 printk("islpci_eth_transmit:wds_mac\n");
146 memmove(skb->data + 6, src, skb->len);
147 memcpy(skb->data, wds_mac, 6);
149 memmove(skb->data, src, skb->len);
152 #if VERBOSE > SHOW_ERROR_MESSAGES
153 DEBUG(SHOW_TRACING, "memmove %p %p %i \n", skb->data,
158 dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
159 newskb_offset = (4 - (long) newskb->data) & 0x03;
161 /* Check if newskb->data is aligned */
163 skb_reserve(newskb, newskb_offset);
165 skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
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");
173 memcpy(newskb->data, skb->data, skb->len);
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);
180 newskb->dev = skb->dev;
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);
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,
195 if (unlikely(pci_map_address == 0)) {
196 printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
202 /* Place the fragment in the control block structure. */
203 index = curr_frag % ISL38XX_CB_TX_QSIZE;
204 fragment = &cb->tx_data_low[index];
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 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);
215 /* The fragment address in the control block must have been
216 * written before announcing the frame buffer to device. */
218 cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
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);
225 /* set the full flag for the transmission queue */
226 priv->data_low_tx_full = 1;
229 /* trigger the device */
230 islpci_trigger(priv);
232 /* unlock the driver code */
233 spin_unlock_irqrestore(&priv->slock, flags);
235 /* set the transmission time */
236 ndev->trans_start = jiffies;
237 priv->statistics.tx_packets++;
238 priv->statistics.tx_bytes += skb->len;
243 /* free the skbuf structure before aborting */
247 priv->statistics.tx_dropped++;
248 spin_unlock_irqrestore(&priv->slock, flags);
253 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
255 /* The card reports full 802.11 packets but with a 20 bytes
256 * header and without the FCS. But there a is a bit that
257 * indicates if the packet is corrupted :-) */
258 struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
259 if (hdr->flags & 0x01)
260 /* This one is bad. Drop it ! */
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);
267 u16 freq = le16_to_cpu(hdr->freq);
270 skb_pull(*skb, sizeof (struct rfmon_header));
272 if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
273 struct sk_buff *newskb = skb_copy_expand(*skb,
278 dev_kfree_skb_irq(*skb);
282 /* This behavior is not very subtile... */
285 /* make room for the new header and fill it. */
287 (struct avs_80211_1_header *) skb_push(*skb,
289 avs_80211_1_header));
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 */
306 skb_pull(*skb, sizeof (struct rfmon_header));
308 (*skb)->protocol = htons(ETH_P_802_2);
309 (*skb)->mac.raw = (*skb)->data;
310 (*skb)->pkt_type = PACKET_OTHERHOST;
316 islpci_eth_receive(islpci_private *priv)
318 struct net_device *ndev = priv->ndev;
319 isl38xx_control_block *control_block = priv->control_block;
326 #if VERBOSE > SHOW_ERROR_MESSAGES
327 DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
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;
339 #if VERBOSE > SHOW_ERROR_MESSAGES
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);
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);
351 /* update the skb structure and allign the buffer */
354 /* shift the buffer allocation offset bytes to get the right frame */
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);
364 /* check whether WDS is enabled and whether the data frame is a WDS frame */
367 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
369 memmove(skb->data, src, skb->len - 6);
370 skb_trim(skb, skb->len - 6);
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);
376 /* display the buffer contents for debugging */
377 DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
378 display_buffer((char *) skb->data, skb->len);
381 /* do some additional sk_buff and network layer parameters */
384 /* take care of monitor mode and spy monitoring. */
385 if (unlikely(priv->iw_mode == IW_MODE_MONITOR))
386 discard = islpci_monitor_rx(priv, &skb);
388 if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
389 /* The packet has a rx_annex. Read it for spy monitoring, Then
390 * remove it, while keeping the 2 leading MAC addr.
392 struct iw_quality wstats;
393 struct rx_annex_header *annex =
394 (struct rx_annex_header *) skb->data;
395 wstats.level = annex->rfmon.rssi;
396 /* The noise value can be a bit outdated if nobody's
397 * reading wireless stats... */
398 wstats.noise = priv->local_iwstatistics.qual.noise;
399 wstats.qual = wstats.level - wstats.noise;
400 wstats.updated = 0x07;
401 /* Update spy records */
402 wireless_spy_update(ndev, annex->addr2, &wstats);
404 memcpy(skb->data + sizeof (struct rfmon_header),
405 skb->data, 2 * ETH_ALEN);
406 skb_pull(skb, sizeof (struct rfmon_header));
408 skb->protocol = eth_type_trans(skb, ndev);
410 skb->ip_summed = CHECKSUM_NONE;
411 priv->statistics.rx_packets++;
412 priv->statistics.rx_bytes += size;
414 /* deliver the skb to the network layer */
415 #ifdef ISLPCI_ETH_DEBUG
417 ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
418 skb->data[0], skb->data[1], skb->data[2], skb->data[3],
419 skb->data[4], skb->data[5]);
421 if (unlikely(discard)) {
422 dev_kfree_skb_irq(skb);
427 /* increment the read index for the rx data low queue */
428 priv->free_data_rx++;
430 /* add one or more sk_buff structures */
432 le32_to_cpu(control_block->
433 driver_curr_frag[ISL38XX_CB_RX_DATA_LQ]),
434 index - priv->free_data_rx < ISL38XX_CB_RX_QSIZE) {
435 /* allocate an sk_buff for received data frames storage
436 * include any required allignment operations */
437 skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2);
438 if (unlikely(skb == NULL)) {
439 /* error allocating an sk_buff structure elements */
440 DEBUG(SHOW_ERROR_MESSAGES, "Error allocating skb \n");
443 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
444 /* store the new skb structure pointer */
445 index = index % ISL38XX_CB_RX_QSIZE;
446 priv->data_low_rx[index] = skb;
448 #if VERBOSE > SHOW_ERROR_MESSAGES
450 "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n ",
451 skb, skb->data, skb->len, index, skb->truesize);
454 /* set the streaming DMA mapping for proper PCI bus operation */
455 priv->pci_map_rx_address[index] =
456 pci_map_single(priv->pdev, (void *) skb->data,
457 MAX_FRAGMENT_SIZE_RX + 2,
459 if (unlikely(priv->pci_map_rx_address[index] == (dma_addr_t) NULL)) {
460 /* error mapping the buffer to device accessable memory address */
461 DEBUG(SHOW_ERROR_MESSAGES,
462 "Error mapping DMA address\n");
464 /* free the skbuf structure before aborting */
465 dev_kfree_skb_irq((struct sk_buff *) skb);
469 /* update the fragment address */
470 control_block->rx_data_low[index].address = cpu_to_le32((u32)
476 /* increment the driver read pointer */
477 add_le32p((u32 *) &control_block->
478 driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
481 /* trigger the device */
482 islpci_trigger(priv);
488 islpci_do_reset_and_wake(void *data)
490 islpci_private *priv = (islpci_private *) data;
491 islpci_reset(priv, 1);
492 netif_wake_queue(priv->ndev);
493 priv->reset_task_pending = 0;
497 islpci_eth_tx_timeout(struct net_device *ndev)
499 islpci_private *priv = netdev_priv(ndev);
500 struct net_device_stats *statistics = &priv->statistics;
502 /* increment the transmit error counter */
503 statistics->tx_errors++;
505 if (!priv->reset_task_pending) {
506 priv->reset_task_pending = 1;
507 netif_stop_queue(ndev);
508 schedule_work(&priv->reset_task);