1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2002 Intersil Americas Inc.
4  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/gfp.h>
9 
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>
16 
17 #include "prismcompat.h"
18 #include "isl_38xx.h"
19 #include "islpci_eth.h"
20 #include "islpci_mgt.h"
21 #include "oid_mgt.h"
22 
23 /******************************************************************************
24     Network Interface functions
25 ******************************************************************************/
26 void
islpci_eth_cleanup_transmit(islpci_private * priv,isl38xx_control_block * control_block)27 islpci_eth_cleanup_transmit(islpci_private *priv,
28 			    isl38xx_control_block *control_block)
29 {
30 	struct sk_buff *skb;
31 	u32 index;
32 
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;
39 
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];
46 
47 #if VERBOSE > SHOW_ERROR_MESSAGES
48 			DEBUG(SHOW_TRACING,
49 			      "cleanup skb %p skb->data %p skb->len %u truesize %u\n",
50 			      skb, skb->data, skb->len, skb->truesize);
51 #endif
52 
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);
57 			skb = NULL;
58 		}
59 		/* increment the free data low queue pointer */
60 		priv->free_data_tx++;
61 	}
62 }
63 
64 netdev_tx_t
islpci_eth_transmit(struct sk_buff * skb,struct net_device * ndev)65 islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
66 {
67 	islpci_private *priv = netdev_priv(ndev);
68 	isl38xx_control_block *cb = priv->control_block;
69 	u32 index;
70 	dma_addr_t pci_map_address;
71 	int frame_size;
72 	isl38xx_fragment *fragment;
73 	int offset;
74 	struct sk_buff *newskb;
75 	int newskb_offset;
76 	unsigned long flags;
77 	unsigned char wds_mac[6];
78 	u32 curr_frag;
79 
80 #if VERBOSE > SHOW_ERROR_MESSAGES
81 	DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit\n");
82 #endif
83 
84 	/* lock the driver code */
85 	spin_lock_irqsave(&priv->slock, flags);
86 
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",
91 		       ndev->name);
92 		netif_stop_queue(ndev);
93 
94 		/* trigger the device */
95 		isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
96 				  ISL38XX_DEV_INT_REG);
97 		udelay(ISL38XX_WRITEIO_DELAY);
98 		goto drop_free;
99 	}
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;
107 
108 		/* check whether the current skb can be used  */
109 		if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
110 			unsigned char *src = skb->data;
111 
112 #if VERBOSE > SHOW_ERROR_MESSAGES
113 			DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
114 			      init_wds);
115 #endif
116 
117 			/* align the buffer on 4-byte boundary */
118 			skb_reserve(skb, (4 - (long) skb->data) & 0x03);
119 			if (init_wds) {
120 				/* wds requires an additional address field of 6 bytes */
121 				skb_put(skb, 6);
122 #ifdef ISLPCI_ETH_DEBUG
123 				printk("islpci_eth_transmit:wds_mac\n");
124 #endif
125 				memmove(skb->data + 6, src, skb->len);
126 				skb_copy_to_linear_data(skb, wds_mac, 6);
127 			} else {
128 				memmove(skb->data, src, skb->len);
129 			}
130 
131 #if VERBOSE > SHOW_ERROR_MESSAGES
132 			DEBUG(SHOW_TRACING, "memmove %p %p %i\n", skb->data,
133 			      src, skb->len);
134 #endif
135 		} else {
136 			newskb =
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",
140 				       ndev->name);
141 				goto drop_free;
142 			}
143 			newskb_offset = (4 - (long) newskb->data) & 0x03;
144 
145 			/* Check if newskb->data is aligned */
146 			if (newskb_offset)
147 				skb_reserve(newskb, newskb_offset);
148 
149 			skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
150 			if (init_wds) {
151 				skb_copy_from_linear_data(skb,
152 							  newskb->data + 6,
153 							  skb->len);
154 				skb_copy_to_linear_data(newskb, wds_mac, 6);
155 #ifdef ISLPCI_ETH_DEBUG
156 				printk("islpci_eth_transmit:wds_mac\n");
157 #endif
158 			} else
159 				skb_copy_from_linear_data(skb, newskb->data,
160 							  skb->len);
161 
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);
165 #endif
166 
167 			newskb->dev = skb->dev;
168 			dev_kfree_skb_irq(skb);
169 			skb = newskb;
170 		}
171 	}
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);
176 #endif
177 
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,
181 					 PCI_DMA_TODEVICE);
182 	if (pci_dma_mapping_error(priv->pdev, pci_map_address)) {
183 		printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
184 		       ndev->name);
185 		goto drop_free;
186 	}
187 	/* Place the fragment in the control block structure. */
188 	index = curr_frag % ISL38XX_CB_TX_QSIZE;
189 	fragment = &cb->tx_data_low[index];
190 
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);
199 	curr_frag++;
200 
201 	/* The fragment address in the control block must have been
202 	 * written before announcing the frame buffer to device. */
203 	wmb();
204 	cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
205 
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);
210 
211 		/* set the full flag for the transmission queue */
212 		priv->data_low_tx_full = 1;
213 	}
214 
215 	ndev->stats.tx_packets++;
216 	ndev->stats.tx_bytes += skb->len;
217 
218 	/* trigger the device */
219 	islpci_trigger(priv);
220 
221 	/* unlock the driver code */
222 	spin_unlock_irqrestore(&priv->slock, flags);
223 
224 	return NETDEV_TX_OK;
225 
226       drop_free:
227 	ndev->stats.tx_dropped++;
228 	spin_unlock_irqrestore(&priv->slock, flags);
229 	dev_kfree_skb(skb);
230 	return NETDEV_TX_OK;
231 }
232 
233 static inline int
islpci_monitor_rx(islpci_private * priv,struct sk_buff ** skb)234 islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
235 {
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;
240 
241 	if (hdr->flags & 0x01)
242 		/* This one is bad. Drop it ! */
243 		return -1;
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);
248 		u8 rate = hdr->rate;
249 		u16 freq = le16_to_cpu(hdr->freq);
250 		u8 rssi = hdr->rssi;
251 
252 		skb_pull(*skb, sizeof (struct rfmon_header));
253 
254 		if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
255 			struct sk_buff *newskb = skb_copy_expand(*skb,
256 								 sizeof (struct
257 									 avs_80211_1_header),
258 								 0, GFP_ATOMIC);
259 			if (newskb) {
260 				dev_kfree_skb_irq(*skb);
261 				*skb = newskb;
262 			} else
263 				return -1;
264 			/* This behavior is not very subtile... */
265 		}
266 
267 		/* make room for the new header and fill it. */
268 		avs = skb_push(*skb, sizeof(struct avs_80211_1_header));
269 
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 */
284 	} else
285 		skb_pull(*skb, sizeof (struct rfmon_header));
286 
287 	(*skb)->protocol = htons(ETH_P_802_2);
288 	skb_reset_mac_header(*skb);
289 	(*skb)->pkt_type = PACKET_OTHERHOST;
290 
291 	return 0;
292 }
293 
294 int
islpci_eth_receive(islpci_private * priv)295 islpci_eth_receive(islpci_private *priv)
296 {
297 	struct net_device *ndev = priv->ndev;
298 	isl38xx_control_block *control_block = priv->control_block;
299 	struct sk_buff *skb;
300 	u16 size;
301 	u32 index, offset;
302 	unsigned char *src;
303 	int discard = 0;
304 
305 #if VERBOSE > SHOW_ERROR_MESSAGES
306 	DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive\n");
307 #endif
308 
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;
317 
318 #if VERBOSE > SHOW_ERROR_MESSAGES
319 	DEBUG(SHOW_TRACING,
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);
323 #endif
324 
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);
329 
330 	/* update the skb structure and align the buffer */
331 	skb_put(skb, size);
332 	if (offset) {
333 		/* shift the buffer allocation offset bytes to get the right frame */
334 		skb_pull(skb, 2);
335 		skb_put(skb, 2);
336 	}
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);
341 #endif
342 
343 	/* check whether WDS is enabled and whether the data frame is a WDS frame */
344 
345 	if (init_wds) {
346 		/* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
347 		src = skb->data + 6;
348 		memmove(skb->data, src, skb->len - 6);
349 		skb_trim(skb, skb->len - 6);
350 	}
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);
354 
355 	/* display the buffer contents for debugging */
356 	DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
357 	display_buffer((char *) skb->data, skb->len);
358 #endif
359 	/* take care of monitor mode and spy monitoring. */
360 	if (unlikely(priv->iw_mode == IW_MODE_MONITOR)) {
361 		skb->dev = ndev;
362 		discard = islpci_monitor_rx(priv, &skb);
363 	} else {
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.
367 			 */
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);
379 
380 			skb_copy_from_linear_data(skb,
381 						  (skb->data +
382 						   sizeof(struct rfmon_header)),
383 						  2 * ETH_ALEN);
384 			skb_pull(skb, sizeof (struct rfmon_header));
385 		}
386 		skb->protocol = eth_type_trans(skb, ndev);
387 	}
388 	skb->ip_summed = CHECKSUM_NONE;
389 	ndev->stats.rx_packets++;
390 	ndev->stats.rx_bytes += size;
391 
392 	/* deliver the skb to the network layer */
393 #ifdef ISLPCI_ETH_DEBUG
394 	printk
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]);
398 #endif
399 	if (unlikely(discard)) {
400 		dev_kfree_skb_irq(skb);
401 		skb = NULL;
402 	} else
403 		netif_rx(skb);
404 
405 	/* increment the read index for the rx data low queue */
406 	priv->free_data_rx++;
407 
408 	/* add one or more sk_buff structures */
409 	while (index =
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");
419 			break;
420 		}
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;
425 
426 #if VERBOSE > SHOW_ERROR_MESSAGES
427 		DEBUG(SHOW_TRACING,
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);
430 #endif
431 
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,
436 				   PCI_DMA_FROMDEVICE);
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");
442 
443 			/* free the skbuf structure before aborting */
444 			dev_kfree_skb_irq(skb);
445 			skb = NULL;
446 			break;
447 		}
448 		/* update the fragment address */
449 		control_block->rx_data_low[index].address =
450 			cpu_to_le32((u32)priv->pci_map_rx_address[index]);
451 		wmb();
452 
453 		/* increment the driver read pointer */
454 		le32_add_cpu(&control_block->
455 			     driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
456 	}
457 
458 	/* trigger the device */
459 	islpci_trigger(priv);
460 
461 	return 0;
462 }
463 
464 void
islpci_do_reset_and_wake(struct work_struct * work)465 islpci_do_reset_and_wake(struct work_struct *work)
466 {
467 	islpci_private *priv = container_of(work, islpci_private, reset_task);
468 
469 	islpci_reset(priv, 1);
470 	priv->reset_task_pending = 0;
471 	smp_wmb();
472 	netif_wake_queue(priv->ndev);
473 }
474 
475 void
islpci_eth_tx_timeout(struct net_device * ndev)476 islpci_eth_tx_timeout(struct net_device *ndev)
477 {
478 	islpci_private *priv = netdev_priv(ndev);
479 
480 	/* increment the transmit error counter */
481 	ndev->stats.tx_errors++;
482 
483 	if (!priv->reset_task_pending) {
484 		printk(KERN_WARNING
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);
489 	} else {
490 		printk(KERN_WARNING
491 			"%s: tx_timeout, waiting for reset", ndev->name);
492 	}
493 }
494