1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2002 Intersil Americas Inc.
4 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/delay.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_arp.h>
15 #include <asm/byteorder.h>
17 #include "prismcompat.h"
19 #include "islpci_eth.h"
20 #include "islpci_mgt.h"
23 /******************************************************************************
24 Network Interface functions
25 ******************************************************************************/
27 islpci_eth_cleanup_transmit(islpci_private
*priv
,
28 isl38xx_control_block
*control_block
)
33 /* compare the control block read pointer with the free pointer */
34 while (priv
->free_data_tx
!=
35 le32_to_cpu(control_block
->
36 device_curr_frag
[ISL38XX_CB_TX_DATA_LQ
])) {
37 /* read the index of the first fragment to be freed */
38 index
= priv
->free_data_tx
% ISL38XX_CB_TX_QSIZE
;
40 /* check for holes in the arrays caused by multi fragment frames
41 * searching for the last fragment of a frame */
42 if (priv
->pci_map_tx_address
[index
]) {
43 /* entry is the last fragment of a frame
44 * free the skb structure and unmap pci memory */
45 skb
= priv
->data_low_tx
[index
];
47 #if VERBOSE > SHOW_ERROR_MESSAGES
49 "cleanup skb %p skb->data %p skb->len %u truesize %u\n",
50 skb
, skb
->data
, skb
->len
, skb
->truesize
);
53 dma_unmap_single(&priv
->pdev
->dev
,
54 priv
->pci_map_tx_address
[index
],
55 skb
->len
, DMA_TO_DEVICE
);
56 dev_kfree_skb_irq(skb
);
59 /* increment the free data low queue pointer */
65 islpci_eth_transmit(struct sk_buff
*skb
, struct net_device
*ndev
)
67 islpci_private
*priv
= netdev_priv(ndev
);
68 isl38xx_control_block
*cb
= priv
->control_block
;
70 dma_addr_t pci_map_address
;
72 isl38xx_fragment
*fragment
;
74 struct sk_buff
*newskb
;
77 unsigned char wds_mac
[6];
80 #if VERBOSE > SHOW_ERROR_MESSAGES
81 DEBUG(SHOW_FUNCTION_CALLS
, "islpci_eth_transmit\n");
84 /* lock the driver code */
85 spin_lock_irqsave(&priv
->slock
, flags
);
87 /* check whether the destination queue has enough fragments for the frame */
88 curr_frag
= le32_to_cpu(cb
->driver_curr_frag
[ISL38XX_CB_TX_DATA_LQ
]);
89 if (unlikely(curr_frag
- priv
->free_data_tx
>= ISL38XX_CB_TX_QSIZE
)) {
90 printk(KERN_ERR
"%s: transmit device queue full when awake\n",
92 netif_stop_queue(ndev
);
94 /* trigger the device */
95 isl38xx_w32_flush(priv
->device_base
, ISL38XX_DEV_INT_UPDATE
,
97 udelay(ISL38XX_WRITEIO_DELAY
);
100 /* Check alignment and WDS frame formatting. The start of the packet should
101 * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
102 * and add WDS address information */
103 if (likely(((long) skb
->data
& 0x03) | init_wds
)) {
104 /* get the number of bytes to add and re-align */
105 offset
= (4 - (long) skb
->data
) & 0x03;
106 offset
+= init_wds
? 6 : 0;
108 /* check whether the current skb can be used */
109 if (!skb_cloned(skb
) && (skb_tailroom(skb
) >= offset
)) {
110 unsigned char *src
= skb
->data
;
112 #if VERBOSE > SHOW_ERROR_MESSAGES
113 DEBUG(SHOW_TRACING
, "skb offset %i wds %i\n", offset
,
117 /* align the buffer on 4-byte boundary */
118 skb_reserve(skb
, (4 - (long) skb
->data
) & 0x03);
120 /* wds requires an additional address field of 6 bytes */
122 #ifdef ISLPCI_ETH_DEBUG
123 printk("islpci_eth_transmit:wds_mac\n");
125 memmove(skb
->data
+ 6, src
, skb
->len
);
126 skb_copy_to_linear_data(skb
, wds_mac
, 6);
128 memmove(skb
->data
, src
, skb
->len
);
131 #if VERBOSE > SHOW_ERROR_MESSAGES
132 DEBUG(SHOW_TRACING
, "memmove %p %p %i\n", skb
->data
,
137 dev_alloc_skb(init_wds
? skb
->len
+ 6 : skb
->len
);
138 if (unlikely(newskb
== NULL
)) {
139 printk(KERN_ERR
"%s: Cannot allocate skb\n",
143 newskb_offset
= (4 - (long) newskb
->data
) & 0x03;
145 /* Check if newskb->data is aligned */
147 skb_reserve(newskb
, newskb_offset
);
149 skb_put(newskb
, init_wds
? skb
->len
+ 6 : skb
->len
);
151 skb_copy_from_linear_data(skb
,
154 skb_copy_to_linear_data(newskb
, wds_mac
, 6);
155 #ifdef ISLPCI_ETH_DEBUG
156 printk("islpci_eth_transmit:wds_mac\n");
159 skb_copy_from_linear_data(skb
, newskb
->data
,
162 #if VERBOSE > SHOW_ERROR_MESSAGES
163 DEBUG(SHOW_TRACING
, "memcpy %p %p %i wds %i\n",
164 newskb
->data
, skb
->data
, skb
->len
, init_wds
);
167 newskb
->dev
= skb
->dev
;
168 dev_kfree_skb_irq(skb
);
172 /* display the buffer contents for debugging */
173 #if VERBOSE > SHOW_ERROR_MESSAGES
174 DEBUG(SHOW_BUFFER_CONTENTS
, "\ntx %p ", skb
->data
);
175 display_buffer((char *) skb
->data
, skb
->len
);
178 /* map the skb buffer to pci memory for DMA operation */
179 pci_map_address
= dma_map_single(&priv
->pdev
->dev
, (void *)skb
->data
,
180 skb
->len
, DMA_TO_DEVICE
);
181 if (dma_mapping_error(&priv
->pdev
->dev
, pci_map_address
)) {
182 printk(KERN_WARNING
"%s: cannot map buffer to PCI\n",
186 /* Place the fragment in the control block structure. */
187 index
= curr_frag
% ISL38XX_CB_TX_QSIZE
;
188 fragment
= &cb
->tx_data_low
[index
];
190 priv
->pci_map_tx_address
[index
] = pci_map_address
;
191 /* store the skb address for future freeing */
192 priv
->data_low_tx
[index
] = skb
;
193 /* set the proper fragment start address and size information */
194 frame_size
= skb
->len
;
195 fragment
->size
= cpu_to_le16(frame_size
);
196 fragment
->flags
= cpu_to_le16(0); /* set to 1 if more fragments */
197 fragment
->address
= cpu_to_le32(pci_map_address
);
200 /* The fragment address in the control block must have been
201 * written before announcing the frame buffer to device. */
203 cb
->driver_curr_frag
[ISL38XX_CB_TX_DATA_LQ
] = cpu_to_le32(curr_frag
);
205 if (curr_frag
- priv
->free_data_tx
+ ISL38XX_MIN_QTHRESHOLD
206 > ISL38XX_CB_TX_QSIZE
) {
207 /* stop sends from upper layers */
208 netif_stop_queue(ndev
);
210 /* set the full flag for the transmission queue */
211 priv
->data_low_tx_full
= 1;
214 ndev
->stats
.tx_packets
++;
215 ndev
->stats
.tx_bytes
+= skb
->len
;
217 /* trigger the device */
218 islpci_trigger(priv
);
220 /* unlock the driver code */
221 spin_unlock_irqrestore(&priv
->slock
, flags
);
226 ndev
->stats
.tx_dropped
++;
227 spin_unlock_irqrestore(&priv
->slock
, flags
);
233 islpci_monitor_rx(islpci_private
*priv
, struct sk_buff
**skb
)
235 /* The card reports full 802.11 packets but with a 20 bytes
236 * header and without the FCS. But there a is a bit that
237 * indicates if the packet is corrupted :-) */
238 struct rfmon_header
*hdr
= (struct rfmon_header
*) (*skb
)->data
;
240 if (hdr
->flags
& 0x01)
241 /* This one is bad. Drop it ! */
243 if (priv
->ndev
->type
== ARPHRD_IEEE80211_PRISM
) {
244 struct avs_80211_1_header
*avs
;
245 /* extract the relevant data from the header */
246 u32 clock
= le32_to_cpu(hdr
->clock
);
248 u16 freq
= le16_to_cpu(hdr
->freq
);
251 skb_pull(*skb
, sizeof (struct rfmon_header
));
253 if (skb_headroom(*skb
) < sizeof (struct avs_80211_1_header
)) {
254 struct sk_buff
*newskb
= skb_copy_expand(*skb
,
259 dev_kfree_skb_irq(*skb
);
263 /* This behavior is not very subtile... */
266 /* make room for the new header and fill it. */
267 avs
= skb_push(*skb
, sizeof(struct avs_80211_1_header
));
269 avs
->version
= cpu_to_be32(P80211CAPTURE_VERSION
);
270 avs
->length
= cpu_to_be32(sizeof (struct avs_80211_1_header
));
271 avs
->mactime
= cpu_to_be64(clock
);
272 avs
->hosttime
= cpu_to_be64(jiffies
);
273 avs
->phytype
= cpu_to_be32(6); /*OFDM: 6 for (g), 8 for (a) */
274 avs
->channel
= cpu_to_be32(channel_of_freq(freq
));
275 avs
->datarate
= cpu_to_be32(rate
* 5);
276 avs
->antenna
= cpu_to_be32(0); /*unknown */
277 avs
->priority
= cpu_to_be32(0); /*unknown */
278 avs
->ssi_type
= cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
279 avs
->ssi_signal
= cpu_to_be32(rssi
& 0x7f);
280 avs
->ssi_noise
= cpu_to_be32(priv
->local_iwstatistics
.qual
.noise
); /*better than 'undefined', I assume */
281 avs
->preamble
= cpu_to_be32(0); /*unknown */
282 avs
->encoding
= cpu_to_be32(0); /*unknown */
284 skb_pull(*skb
, sizeof (struct rfmon_header
));
286 (*skb
)->protocol
= htons(ETH_P_802_2
);
287 skb_reset_mac_header(*skb
);
288 (*skb
)->pkt_type
= PACKET_OTHERHOST
;
294 islpci_eth_receive(islpci_private
*priv
)
296 struct net_device
*ndev
= priv
->ndev
;
297 isl38xx_control_block
*control_block
= priv
->control_block
;
304 #if VERBOSE > SHOW_ERROR_MESSAGES
305 DEBUG(SHOW_FUNCTION_CALLS
, "islpci_eth_receive\n");
308 /* the device has written an Ethernet frame in the data area
309 * of the sk_buff without updating the structure, do it now */
310 index
= priv
->free_data_rx
% ISL38XX_CB_RX_QSIZE
;
311 size
= le16_to_cpu(control_block
->rx_data_low
[index
].size
);
312 skb
= priv
->data_low_rx
[index
];
313 offset
= ((unsigned long)
314 le32_to_cpu(control_block
->rx_data_low
[index
].address
) -
315 (unsigned long) skb
->data
) & 3;
317 #if VERBOSE > SHOW_ERROR_MESSAGES
319 "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n",
320 control_block
->rx_data_low
[priv
->free_data_rx
].address
, skb
->data
,
321 skb
->len
, offset
, skb
->truesize
);
324 /* delete the streaming DMA mapping before processing the skb */
325 dma_unmap_single(&priv
->pdev
->dev
, priv
->pci_map_rx_address
[index
],
326 MAX_FRAGMENT_SIZE_RX
+ 2, DMA_FROM_DEVICE
);
328 /* update the skb structure and align the buffer */
331 /* shift the buffer allocation offset bytes to get the right frame */
335 #if VERBOSE > SHOW_ERROR_MESSAGES
336 /* display the buffer contents for debugging */
337 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
338 display_buffer((char *) skb
->data
, skb
->len
);
341 /* check whether WDS is enabled and whether the data frame is a WDS frame */
344 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
346 memmove(skb
->data
, src
, skb
->len
- 6);
347 skb_trim(skb
, skb
->len
- 6);
349 #if VERBOSE > SHOW_ERROR_MESSAGES
350 DEBUG(SHOW_TRACING
, "Fragment size %i in skb at %p\n", size
, skb
);
351 DEBUG(SHOW_TRACING
, "Skb data at %p, length %i\n", skb
->data
, skb
->len
);
353 /* display the buffer contents for debugging */
354 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
355 display_buffer((char *) skb
->data
, skb
->len
);
357 /* take care of monitor mode and spy monitoring. */
358 if (unlikely(priv
->iw_mode
== IW_MODE_MONITOR
)) {
360 discard
= islpci_monitor_rx(priv
, &skb
);
362 if (unlikely(skb
->data
[2 * ETH_ALEN
] == 0)) {
363 /* The packet has a rx_annex. Read it for spy monitoring, Then
364 * remove it, while keeping the 2 leading MAC addr.
366 struct iw_quality wstats
;
367 struct rx_annex_header
*annex
=
368 (struct rx_annex_header
*) skb
->data
;
369 wstats
.level
= annex
->rfmon
.rssi
;
370 /* The noise value can be a bit outdated if nobody's
371 * reading wireless stats... */
372 wstats
.noise
= priv
->local_iwstatistics
.qual
.noise
;
373 wstats
.qual
= wstats
.level
- wstats
.noise
;
374 wstats
.updated
= 0x07;
375 /* Update spy records */
376 wireless_spy_update(ndev
, annex
->addr2
, &wstats
);
378 skb_copy_from_linear_data(skb
,
380 sizeof(struct rfmon_header
)),
382 skb_pull(skb
, sizeof (struct rfmon_header
));
384 skb
->protocol
= eth_type_trans(skb
, ndev
);
386 skb
->ip_summed
= CHECKSUM_NONE
;
387 ndev
->stats
.rx_packets
++;
388 ndev
->stats
.rx_bytes
+= size
;
390 /* deliver the skb to the network layer */
391 #ifdef ISLPCI_ETH_DEBUG
393 ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
394 skb
->data
[0], skb
->data
[1], skb
->data
[2], skb
->data
[3],
395 skb
->data
[4], skb
->data
[5]);
397 if (unlikely(discard
)) {
398 dev_kfree_skb_irq(skb
);
403 /* increment the read index for the rx data low queue */
404 priv
->free_data_rx
++;
406 /* add one or more sk_buff structures */
408 le32_to_cpu(control_block
->
409 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
]),
410 index
- priv
->free_data_rx
< ISL38XX_CB_RX_QSIZE
) {
411 /* allocate an sk_buff for received data frames storage
412 * include any required allignment operations */
413 skb
= dev_alloc_skb(MAX_FRAGMENT_SIZE_RX
+ 2);
414 if (unlikely(skb
== NULL
)) {
415 /* error allocating an sk_buff structure elements */
416 DEBUG(SHOW_ERROR_MESSAGES
, "Error allocating skb\n");
419 skb_reserve(skb
, (4 - (long) skb
->data
) & 0x03);
420 /* store the new skb structure pointer */
421 index
= index
% ISL38XX_CB_RX_QSIZE
;
422 priv
->data_low_rx
[index
] = skb
;
424 #if VERBOSE > SHOW_ERROR_MESSAGES
426 "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n",
427 skb
, skb
->data
, skb
->len
, index
, skb
->truesize
);
430 /* set the streaming DMA mapping for proper PCI bus operation */
431 priv
->pci_map_rx_address
[index
] =
432 dma_map_single(&priv
->pdev
->dev
, (void *)skb
->data
,
433 MAX_FRAGMENT_SIZE_RX
+ 2, DMA_FROM_DEVICE
);
434 if (dma_mapping_error(&priv
->pdev
->dev
, priv
->pci_map_rx_address
[index
])) {
435 /* error mapping the buffer to device accessible memory address */
436 DEBUG(SHOW_ERROR_MESSAGES
,
437 "Error mapping DMA address\n");
439 /* free the skbuf structure before aborting */
440 dev_kfree_skb_irq(skb
);
444 /* update the fragment address */
445 control_block
->rx_data_low
[index
].address
=
446 cpu_to_le32((u32
)priv
->pci_map_rx_address
[index
]);
449 /* increment the driver read pointer */
450 le32_add_cpu(&control_block
->
451 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
], 1);
454 /* trigger the device */
455 islpci_trigger(priv
);
461 islpci_do_reset_and_wake(struct work_struct
*work
)
463 islpci_private
*priv
= container_of(work
, islpci_private
, reset_task
);
465 islpci_reset(priv
, 1);
466 priv
->reset_task_pending
= 0;
468 netif_wake_queue(priv
->ndev
);
472 islpci_eth_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
474 islpci_private
*priv
= netdev_priv(ndev
);
476 /* increment the transmit error counter */
477 ndev
->stats
.tx_errors
++;
479 if (!priv
->reset_task_pending
) {
481 "%s: tx_timeout, scheduling reset", ndev
->name
);
482 netif_stop_queue(ndev
);
483 priv
->reset_task_pending
= 1;
484 schedule_work(&priv
->reset_task
);
487 "%s: tx_timeout, waiting for reset", ndev
->name
);