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>
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] != (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];
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];
92 #if VERBOSE > SHOW_ERROR_MESSAGES
93 DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit \n");
96 /* lock the driver code */
97 spin_lock_irqsave(&priv->slock, flags);
99 /* determine the amount of fragments needed to store the frame */
101 frame_size = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
105 /* check whether the destination queue has enough fragments for the frame */
106 curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ]);
107 if (curr_frag - priv->free_data_tx >= ISL38XX_CB_TX_QSIZE) {
108 printk(KERN_ERR "%s: transmit device queue full when awake\n",
110 netif_stop_queue(ndev);
112 /* trigger the device */
113 isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
114 ISL38XX_DEV_INT_REG);
115 udelay(ISL38XX_WRITEIO_DELAY);
120 /* Check alignment and WDS frame formatting. The start of the packet should
121 * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
122 * and add WDS address information */
123 if (((long) skb->data & 0x03) | init_wds) {
124 /* get the number of bytes to add and re-allign */
125 offset = (4 - (long) skb->data) & 0x03;
126 offset += init_wds ? 6 : 0;
128 /* check whether the current skb can be used */
129 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
130 unsigned char *src = skb->data;
132 #if VERBOSE > SHOW_ERROR_MESSAGES
133 DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
137 /* align the buffer on 4-byte boundary */
138 skb_reserve(skb, (4 - (long) skb->data) & 0x03);
140 /* wds requires an additional address field of 6 bytes */
142 #ifdef ISLPCI_ETH_DEBUG
143 printk("islpci_eth_transmit:wds_mac\n");
145 memmove(skb->data + 6, src, skb->len);
146 memcpy(skb->data, wds_mac, 6);
148 memmove(skb->data, src, skb->len);
151 #if VERBOSE > SHOW_ERROR_MESSAGES
152 DEBUG(SHOW_TRACING, "memmove %p %p %i \n", skb->data,
157 dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
158 newskb_offset = (4 - (long) newskb->data) & 0x03;
160 /* Check if newskb->data is aligned */
162 skb_reserve(newskb, newskb_offset);
164 skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
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");
172 memcpy(newskb->data, skb->data, skb->len);
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);
179 newskb->dev = skb->dev;
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);
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,
194 if (pci_map_address == 0) {
195 printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
201 /* Place the fragment in the control block structure. */
202 index = curr_frag % ISL38XX_CB_TX_QSIZE;
203 fragment = &cb->tx_data_low[index];
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 fragment->size = cpu_to_le16(frame_size);
210 fragment->flags = cpu_to_le16(0); /* set to 1 if more fragments */
211 fragment->address = cpu_to_le32(pci_map_address);
214 /* The fragment address in the control block must have been
215 * written before announcing the frame buffer to device. */
217 cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
219 if (curr_frag - priv->free_data_tx + ISL38XX_MIN_QTHRESHOLD
220 > ISL38XX_CB_TX_QSIZE) {
221 /* stop sends from upper layers */
222 netif_stop_queue(ndev);
224 /* set the full flag for the transmission queue */
225 priv->data_low_tx_full = 1;
228 /* trigger the device */
229 islpci_trigger(priv);
231 /* unlock the driver code */
232 spin_unlock_irqrestore(&priv->slock, flags);
234 /* set the transmission time */
235 ndev->trans_start = jiffies;
236 priv->statistics.tx_packets++;
237 priv->statistics.tx_bytes += skb->len;
242 /* free the skbuf structure before aborting */
246 priv->statistics.tx_dropped++;
247 spin_unlock_irqrestore(&priv->slock, flags);
252 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
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 if (hdr->flags & 0x01)
259 /* This one is bad. Drop it ! */
261 if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
262 struct avs_80211_1_header *avs;
263 /* extract the relevant data from the header */
264 u32 clock = hdr->clock;
266 u16 freq = be16_to_cpu(hdr->freq);
269 skb_pull(*skb, sizeof (struct rfmon_header));
271 if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
272 struct sk_buff *newskb = skb_copy_expand(*skb,
281 /* This behavior is not very subtile... */
284 /* make room for the new header and fill it. */
286 (struct avs_80211_1_header *) skb_push(*skb,
288 avs_80211_1_header));
290 avs->version = htonl(P80211CAPTURE_VERSION);
291 avs->length = htonl(sizeof (struct avs_80211_1_header));
292 avs->mactime = __cpu_to_be64(clock);
293 avs->hosttime = __cpu_to_be64(jiffies);
294 avs->phytype = htonl(6); /*OFDM: 6 for (g), 8 for (a) */
295 avs->channel = htonl(channel_of_freq(freq));
296 avs->datarate = htonl(rate * 5);
297 avs->antenna = htonl(0); /*unknown */
298 avs->priority = htonl(0); /*unknown */
299 avs->ssi_type = htonl(2); /*2: dBm, 3: raw RSSI */
300 avs->ssi_signal = htonl(rssi);
301 avs->ssi_noise = htonl(priv->local_iwstatistics.qual.noise); /*better than 'undefined', I assume */
302 avs->preamble = htonl(0); /*unknown */
303 avs->encoding = htonl(0); /*unknown */
305 skb_pull(*skb, sizeof (struct rfmon_header));
307 (*skb)->protocol = htons(ETH_P_802_2);
308 (*skb)->mac.raw = (*skb)->data;
309 (*skb)->pkt_type = PACKET_OTHERHOST;
315 islpci_eth_receive(islpci_private *priv)
317 struct net_device *ndev = priv->ndev;
318 isl38xx_control_block *control_block = priv->control_block;
325 #if VERBOSE > SHOW_ERROR_MESSAGES
326 DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
329 /* the device has written an Ethernet frame in the data area
330 * of the sk_buff without updating the structure, do it now */
331 index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
332 size = le16_to_cpu(control_block->rx_data_low[index].size);
333 skb = priv->data_low_rx[index];
334 offset = ((unsigned long)
335 le32_to_cpu(control_block->rx_data_low[index].address) -
336 (unsigned long) skb->data) & 3;
338 #if VERBOSE > SHOW_ERROR_MESSAGES
340 "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n ",
341 control_block->rx_data_low[priv->free_data_rx].address, skb->data,
342 skb->len, offset, skb->truesize);
345 /* delete the streaming DMA mapping before processing the skb */
346 pci_unmap_single(priv->pdev,
347 priv->pci_map_rx_address[index],
348 MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
350 /* update the skb structure and allign the buffer */
353 /* shift the buffer allocation offset bytes to get the right frame */
357 #if VERBOSE > SHOW_ERROR_MESSAGES
358 /* display the buffer contents for debugging */
359 DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
360 display_buffer((char *) skb->data, skb->len);
363 /* check whether WDS is enabled and whether the data frame is a WDS frame */
366 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
368 memmove(skb->data, src, skb->len - 6);
369 skb_trim(skb, skb->len - 6);
371 #if VERBOSE > SHOW_ERROR_MESSAGES
372 DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
373 DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
375 /* display the buffer contents for debugging */
376 DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
377 display_buffer((char *) skb->data, skb->len);
380 /* do some additional sk_buff and network layer parameters */
383 /* take care of monitor mode and spy monitoring. */
384 if (priv->iw_mode == IW_MODE_MONITOR)
385 discard = islpci_monitor_rx(priv, &skb);
387 if (skb->data[2 * ETH_ALEN] == 0) {
388 /* The packet has a rx_annex. Read it for spy monitoring, Then
389 * remove it, while keeping the 2 leading MAC addr.
391 struct iw_quality wstats;
392 struct rx_annex_header *annex =
393 (struct rx_annex_header *) skb->data;
394 wstats.level = annex->rfmon.rssi;
395 /* The noise value can be a bit outdated if nobody's
396 * reading wireless stats... */
397 wstats.noise = priv->local_iwstatistics.qual.noise;
398 wstats.qual = wstats.level - wstats.noise;
399 wstats.updated = 0x07;
400 /* Update spy records */
401 wireless_spy_update(ndev, annex->addr2, &wstats);
403 memcpy(skb->data + sizeof (struct rfmon_header),
404 skb->data, 2 * ETH_ALEN);
405 skb_pull(skb, sizeof (struct rfmon_header));
407 skb->protocol = eth_type_trans(skb, ndev);
409 skb->ip_summed = CHECKSUM_NONE;
410 priv->statistics.rx_packets++;
411 priv->statistics.rx_bytes += size;
413 /* deliver the skb to the network layer */
414 #ifdef ISLPCI_ETH_DEBUG
416 ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
417 skb->data[0], skb->data[1], skb->data[2], skb->data[3],
418 skb->data[4], skb->data[5]);
426 /* increment the read index for the rx data low queue */
427 priv->free_data_rx++;
429 /* add one or more sk_buff structures */
431 le32_to_cpu(control_block->
432 driver_curr_frag[ISL38XX_CB_RX_DATA_LQ]),
433 index - priv->free_data_rx < ISL38XX_CB_RX_QSIZE) {
434 /* allocate an sk_buff for received data frames storage
435 * include any required allignment operations */
436 if (skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2), skb == NULL) {
437 /* error allocating an sk_buff structure elements */
438 DEBUG(SHOW_ERROR_MESSAGES, "Error allocating skb \n");
441 /* store the new skb structure pointer */
442 index = index % ISL38XX_CB_RX_QSIZE;
443 priv->data_low_rx[index] = skb;
445 #if VERBOSE > SHOW_ERROR_MESSAGES
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);
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,
456 if (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");
461 /* free the skbuf structure before aborting */
462 dev_kfree_skb((struct sk_buff *) skb);
466 /* update the fragment address */
467 control_block->rx_data_low[index].address = cpu_to_le32((u32)
473 /* increment the driver read pointer */
474 add_le32p((u32 *) &control_block->
475 driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
478 /* trigger the device */
479 islpci_trigger(priv);
485 islpci_do_reset_and_wake(void *data)
487 islpci_private *priv = (islpci_private *) data;
488 islpci_reset(priv, 1);
489 netif_wake_queue(priv->ndev);
490 priv->reset_task_pending = 0;
494 islpci_eth_tx_timeout(struct net_device *ndev)
496 islpci_private *priv = netdev_priv(ndev);
497 struct net_device_stats *statistics = &priv->statistics;
499 /* increment the transmit error counter */
500 statistics->tx_errors++;
502 if (!priv->reset_task_pending) {
503 priv->reset_task_pending = 1;
504 netif_stop_queue(ndev);
505 schedule_work(&priv->reset_task);