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 pci_unmap_single(priv
->pdev
,
54 priv
->pci_map_tx_address
[index
],
55 skb
->len
, PCI_DMA_TODEVICE
);
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
= pci_map_single(priv
->pdev
,
180 (void *) skb
->data
, skb
->len
,
182 if (pci_dma_mapping_error(priv
->pdev
, pci_map_address
)) {
183 printk(KERN_WARNING
"%s: cannot map buffer to PCI\n",
187 /* Place the fragment in the control block structure. */
188 index
= curr_frag
% ISL38XX_CB_TX_QSIZE
;
189 fragment
= &cb
->tx_data_low
[index
];
191 priv
->pci_map_tx_address
[index
] = pci_map_address
;
192 /* store the skb address for future freeing */
193 priv
->data_low_tx
[index
] = skb
;
194 /* set the proper fragment start address and size information */
195 frame_size
= skb
->len
;
196 fragment
->size
= cpu_to_le16(frame_size
);
197 fragment
->flags
= cpu_to_le16(0); /* set to 1 if more fragments */
198 fragment
->address
= cpu_to_le32(pci_map_address
);
201 /* The fragment address in the control block must have been
202 * written before announcing the frame buffer to device. */
204 cb
->driver_curr_frag
[ISL38XX_CB_TX_DATA_LQ
] = cpu_to_le32(curr_frag
);
206 if (curr_frag
- priv
->free_data_tx
+ ISL38XX_MIN_QTHRESHOLD
207 > ISL38XX_CB_TX_QSIZE
) {
208 /* stop sends from upper layers */
209 netif_stop_queue(ndev
);
211 /* set the full flag for the transmission queue */
212 priv
->data_low_tx_full
= 1;
215 ndev
->stats
.tx_packets
++;
216 ndev
->stats
.tx_bytes
+= skb
->len
;
218 /* trigger the device */
219 islpci_trigger(priv
);
221 /* unlock the driver code */
222 spin_unlock_irqrestore(&priv
->slock
, flags
);
227 ndev
->stats
.tx_dropped
++;
228 spin_unlock_irqrestore(&priv
->slock
, flags
);
234 islpci_monitor_rx(islpci_private
*priv
, struct sk_buff
**skb
)
236 /* The card reports full 802.11 packets but with a 20 bytes
237 * header and without the FCS. But there a is a bit that
238 * indicates if the packet is corrupted :-) */
239 struct rfmon_header
*hdr
= (struct rfmon_header
*) (*skb
)->data
;
241 if (hdr
->flags
& 0x01)
242 /* This one is bad. Drop it ! */
244 if (priv
->ndev
->type
== ARPHRD_IEEE80211_PRISM
) {
245 struct avs_80211_1_header
*avs
;
246 /* extract the relevant data from the header */
247 u32 clock
= le32_to_cpu(hdr
->clock
);
249 u16 freq
= le16_to_cpu(hdr
->freq
);
252 skb_pull(*skb
, sizeof (struct rfmon_header
));
254 if (skb_headroom(*skb
) < sizeof (struct avs_80211_1_header
)) {
255 struct sk_buff
*newskb
= skb_copy_expand(*skb
,
260 dev_kfree_skb_irq(*skb
);
264 /* This behavior is not very subtile... */
267 /* make room for the new header and fill it. */
268 avs
= skb_push(*skb
, sizeof(struct avs_80211_1_header
));
270 avs
->version
= cpu_to_be32(P80211CAPTURE_VERSION
);
271 avs
->length
= cpu_to_be32(sizeof (struct avs_80211_1_header
));
272 avs
->mactime
= cpu_to_be64(clock
);
273 avs
->hosttime
= cpu_to_be64(jiffies
);
274 avs
->phytype
= cpu_to_be32(6); /*OFDM: 6 for (g), 8 for (a) */
275 avs
->channel
= cpu_to_be32(channel_of_freq(freq
));
276 avs
->datarate
= cpu_to_be32(rate
* 5);
277 avs
->antenna
= cpu_to_be32(0); /*unknown */
278 avs
->priority
= cpu_to_be32(0); /*unknown */
279 avs
->ssi_type
= cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
280 avs
->ssi_signal
= cpu_to_be32(rssi
& 0x7f);
281 avs
->ssi_noise
= cpu_to_be32(priv
->local_iwstatistics
.qual
.noise
); /*better than 'undefined', I assume */
282 avs
->preamble
= cpu_to_be32(0); /*unknown */
283 avs
->encoding
= cpu_to_be32(0); /*unknown */
285 skb_pull(*skb
, sizeof (struct rfmon_header
));
287 (*skb
)->protocol
= htons(ETH_P_802_2
);
288 skb_reset_mac_header(*skb
);
289 (*skb
)->pkt_type
= PACKET_OTHERHOST
;
295 islpci_eth_receive(islpci_private
*priv
)
297 struct net_device
*ndev
= priv
->ndev
;
298 isl38xx_control_block
*control_block
= priv
->control_block
;
305 #if VERBOSE > SHOW_ERROR_MESSAGES
306 DEBUG(SHOW_FUNCTION_CALLS
, "islpci_eth_receive\n");
309 /* the device has written an Ethernet frame in the data area
310 * of the sk_buff without updating the structure, do it now */
311 index
= priv
->free_data_rx
% ISL38XX_CB_RX_QSIZE
;
312 size
= le16_to_cpu(control_block
->rx_data_low
[index
].size
);
313 skb
= priv
->data_low_rx
[index
];
314 offset
= ((unsigned long)
315 le32_to_cpu(control_block
->rx_data_low
[index
].address
) -
316 (unsigned long) skb
->data
) & 3;
318 #if VERBOSE > SHOW_ERROR_MESSAGES
320 "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n",
321 control_block
->rx_data_low
[priv
->free_data_rx
].address
, skb
->data
,
322 skb
->len
, offset
, skb
->truesize
);
325 /* delete the streaming DMA mapping before processing the skb */
326 pci_unmap_single(priv
->pdev
,
327 priv
->pci_map_rx_address
[index
],
328 MAX_FRAGMENT_SIZE_RX
+ 2, PCI_DMA_FROMDEVICE
);
330 /* update the skb structure and align the buffer */
333 /* shift the buffer allocation offset bytes to get the right frame */
337 #if VERBOSE > SHOW_ERROR_MESSAGES
338 /* display the buffer contents for debugging */
339 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
340 display_buffer((char *) skb
->data
, skb
->len
);
343 /* check whether WDS is enabled and whether the data frame is a WDS frame */
346 /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
348 memmove(skb
->data
, src
, skb
->len
- 6);
349 skb_trim(skb
, skb
->len
- 6);
351 #if VERBOSE > SHOW_ERROR_MESSAGES
352 DEBUG(SHOW_TRACING
, "Fragment size %i in skb at %p\n", size
, skb
);
353 DEBUG(SHOW_TRACING
, "Skb data at %p, length %i\n", skb
->data
, skb
->len
);
355 /* display the buffer contents for debugging */
356 DEBUG(SHOW_BUFFER_CONTENTS
, "\nrx %p ", skb
->data
);
357 display_buffer((char *) skb
->data
, skb
->len
);
359 /* take care of monitor mode and spy monitoring. */
360 if (unlikely(priv
->iw_mode
== IW_MODE_MONITOR
)) {
362 discard
= islpci_monitor_rx(priv
, &skb
);
364 if (unlikely(skb
->data
[2 * ETH_ALEN
] == 0)) {
365 /* The packet has a rx_annex. Read it for spy monitoring, Then
366 * remove it, while keeping the 2 leading MAC addr.
368 struct iw_quality wstats
;
369 struct rx_annex_header
*annex
=
370 (struct rx_annex_header
*) skb
->data
;
371 wstats
.level
= annex
->rfmon
.rssi
;
372 /* The noise value can be a bit outdated if nobody's
373 * reading wireless stats... */
374 wstats
.noise
= priv
->local_iwstatistics
.qual
.noise
;
375 wstats
.qual
= wstats
.level
- wstats
.noise
;
376 wstats
.updated
= 0x07;
377 /* Update spy records */
378 wireless_spy_update(ndev
, annex
->addr2
, &wstats
);
380 skb_copy_from_linear_data(skb
,
382 sizeof(struct rfmon_header
)),
384 skb_pull(skb
, sizeof (struct rfmon_header
));
386 skb
->protocol
= eth_type_trans(skb
, ndev
);
388 skb
->ip_summed
= CHECKSUM_NONE
;
389 ndev
->stats
.rx_packets
++;
390 ndev
->stats
.rx_bytes
+= size
;
392 /* deliver the skb to the network layer */
393 #ifdef ISLPCI_ETH_DEBUG
395 ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
396 skb
->data
[0], skb
->data
[1], skb
->data
[2], skb
->data
[3],
397 skb
->data
[4], skb
->data
[5]);
399 if (unlikely(discard
)) {
400 dev_kfree_skb_irq(skb
);
405 /* increment the read index for the rx data low queue */
406 priv
->free_data_rx
++;
408 /* add one or more sk_buff structures */
410 le32_to_cpu(control_block
->
411 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
]),
412 index
- priv
->free_data_rx
< ISL38XX_CB_RX_QSIZE
) {
413 /* allocate an sk_buff for received data frames storage
414 * include any required allignment operations */
415 skb
= dev_alloc_skb(MAX_FRAGMENT_SIZE_RX
+ 2);
416 if (unlikely(skb
== NULL
)) {
417 /* error allocating an sk_buff structure elements */
418 DEBUG(SHOW_ERROR_MESSAGES
, "Error allocating skb\n");
421 skb_reserve(skb
, (4 - (long) skb
->data
) & 0x03);
422 /* store the new skb structure pointer */
423 index
= index
% ISL38XX_CB_RX_QSIZE
;
424 priv
->data_low_rx
[index
] = skb
;
426 #if VERBOSE > SHOW_ERROR_MESSAGES
428 "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n",
429 skb
, skb
->data
, skb
->len
, index
, skb
->truesize
);
432 /* set the streaming DMA mapping for proper PCI bus operation */
433 priv
->pci_map_rx_address
[index
] =
434 pci_map_single(priv
->pdev
, (void *) skb
->data
,
435 MAX_FRAGMENT_SIZE_RX
+ 2,
437 if (pci_dma_mapping_error(priv
->pdev
,
438 priv
->pci_map_rx_address
[index
])) {
439 /* error mapping the buffer to device accessible memory address */
440 DEBUG(SHOW_ERROR_MESSAGES
,
441 "Error mapping DMA address\n");
443 /* free the skbuf structure before aborting */
444 dev_kfree_skb_irq(skb
);
448 /* update the fragment address */
449 control_block
->rx_data_low
[index
].address
=
450 cpu_to_le32((u32
)priv
->pci_map_rx_address
[index
]);
453 /* increment the driver read pointer */
454 le32_add_cpu(&control_block
->
455 driver_curr_frag
[ISL38XX_CB_RX_DATA_LQ
], 1);
458 /* trigger the device */
459 islpci_trigger(priv
);
465 islpci_do_reset_and_wake(struct work_struct
*work
)
467 islpci_private
*priv
= container_of(work
, islpci_private
, reset_task
);
469 islpci_reset(priv
, 1);
470 priv
->reset_task_pending
= 0;
472 netif_wake_queue(priv
->ndev
);
476 islpci_eth_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
478 islpci_private
*priv
= netdev_priv(ndev
);
480 /* increment the transmit error counter */
481 ndev
->stats
.tx_errors
++;
483 if (!priv
->reset_task_pending
) {
485 "%s: tx_timeout, scheduling reset", ndev
->name
);
486 netif_stop_queue(ndev
);
487 priv
->reset_task_pending
= 1;
488 schedule_work(&priv
->reset_task
);
491 "%s: tx_timeout, waiting for reset", ndev
->name
);