xref: /linux/drivers/net/ethernet/aeroflex/greth.c (revision 07d6bf634bc8f93caf8920c9d61df761645336e2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
4  *
5  * 2005-2010 (c) Aeroflex Gaisler AB
6  *
7  * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
8  * available in the GRLIB VHDL IP core library.
9  *
10  * Full documentation of both cores can be found here:
11  * https://www.gaisler.com/products/grlib/grip.pdf
12  *
13  * The Gigabit version supports scatter/gather DMA, any alignment of
14  * buffers and checksum offloading.
15  *
16  * Contributors: Kristoffer Glembo
17  *               Daniel Hellstrom
18  *               Marko Isomaki
19  */
20 
21 #include <linux/dma-mapping.h>
22 #include <linux/module.h>
23 #include <linux/uaccess.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/skbuff.h>
29 #include <linux/io.h>
30 #include <linux/crc32.h>
31 #include <linux/mii.h>
32 #include <linux/of.h>
33 #include <linux/of_net.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
36 #include <asm/cacheflush.h>
37 #include <asm/byteorder.h>
38 
39 #ifdef CONFIG_SPARC
40 #include <asm/idprom.h>
41 #endif
42 
43 #include "greth.h"
44 
45 #define GRETH_DEF_MSG_ENABLE	  \
46 	(NETIF_MSG_DRV		| \
47 	 NETIF_MSG_PROBE	| \
48 	 NETIF_MSG_LINK		| \
49 	 NETIF_MSG_IFDOWN	| \
50 	 NETIF_MSG_IFUP		| \
51 	 NETIF_MSG_RX_ERR	| \
52 	 NETIF_MSG_TX_ERR)
53 
54 static int greth_debug = -1;	/* -1 == use GRETH_DEF_MSG_ENABLE as value */
55 module_param(greth_debug, int, 0);
56 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
57 
58 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
59 static int macaddr[6];
60 module_param_array(macaddr, int, NULL, 0);
61 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
62 
63 static int greth_edcl = 1;
64 module_param(greth_edcl, int, 0);
65 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
66 
67 static int greth_open(struct net_device *dev);
68 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
69 	   struct net_device *dev);
70 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
71 	   struct net_device *dev);
72 static int greth_rx(struct net_device *dev, int limit);
73 static int greth_rx_gbit(struct net_device *dev, int limit);
74 static void greth_clean_tx(struct net_device *dev);
75 static void greth_clean_tx_gbit(struct net_device *dev);
76 static irqreturn_t greth_interrupt(int irq, void *dev_id);
77 static int greth_close(struct net_device *dev);
78 static int greth_set_mac_add(struct net_device *dev, void *p);
79 static void greth_set_multicast_list(struct net_device *dev);
80 
81 #define GRETH_REGLOAD(a)	    (be32_to_cpu(__raw_readl(&(a))))
82 #define GRETH_REGSAVE(a, v)         (__raw_writel(cpu_to_be32(v), &(a)))
83 #define GRETH_REGORIN(a, v)         (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
84 #define GRETH_REGANDIN(a, v)        (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
85 
86 #define NEXT_TX(N)      (((N) + 1) & GRETH_TXBD_NUM_MASK)
87 #define SKIP_TX(N, C)   (((N) + C) & GRETH_TXBD_NUM_MASK)
88 #define NEXT_RX(N)      (((N) + 1) & GRETH_RXBD_NUM_MASK)
89 
greth_print_rx_packet(void * addr,int len)90 static void greth_print_rx_packet(void *addr, int len)
91 {
92 	print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
93 			addr, len, true);
94 }
95 
greth_print_tx_packet(struct sk_buff * skb)96 static void greth_print_tx_packet(struct sk_buff *skb)
97 {
98 	int i;
99 	int length;
100 
101 	if (skb_shinfo(skb)->nr_frags == 0)
102 		length = skb->len;
103 	else
104 		length = skb_headlen(skb);
105 
106 	print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
107 			skb->data, length, true);
108 
109 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
110 
111 		print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
112 			       skb_frag_address(&skb_shinfo(skb)->frags[i]),
113 			       skb_frag_size(&skb_shinfo(skb)->frags[i]), true);
114 	}
115 }
116 
greth_enable_tx(struct greth_private * greth)117 static inline void greth_enable_tx(struct greth_private *greth)
118 {
119 	wmb();
120 	GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
121 }
122 
greth_enable_tx_and_irq(struct greth_private * greth)123 static inline void greth_enable_tx_and_irq(struct greth_private *greth)
124 {
125 	wmb(); /* BDs must been written to memory before enabling TX */
126 	GRETH_REGORIN(greth->regs->control, GRETH_TXEN | GRETH_TXI);
127 }
128 
greth_disable_tx(struct greth_private * greth)129 static inline void greth_disable_tx(struct greth_private *greth)
130 {
131 	GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
132 }
133 
greth_enable_rx(struct greth_private * greth)134 static inline void greth_enable_rx(struct greth_private *greth)
135 {
136 	wmb();
137 	GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
138 }
139 
greth_disable_rx(struct greth_private * greth)140 static inline void greth_disable_rx(struct greth_private *greth)
141 {
142 	GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
143 }
144 
greth_enable_irqs(struct greth_private * greth)145 static inline void greth_enable_irqs(struct greth_private *greth)
146 {
147 	GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
148 }
149 
greth_disable_irqs(struct greth_private * greth)150 static inline void greth_disable_irqs(struct greth_private *greth)
151 {
152 	GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
153 }
154 
greth_write_bd(u32 * bd,u32 val)155 static inline void greth_write_bd(u32 *bd, u32 val)
156 {
157 	__raw_writel(cpu_to_be32(val), bd);
158 }
159 
greth_read_bd(u32 * bd)160 static inline u32 greth_read_bd(u32 *bd)
161 {
162 	return be32_to_cpu(__raw_readl(bd));
163 }
164 
greth_clean_rings(struct greth_private * greth)165 static void greth_clean_rings(struct greth_private *greth)
166 {
167 	int i;
168 	struct greth_bd *rx_bdp = greth->rx_bd_base;
169 	struct greth_bd *tx_bdp = greth->tx_bd_base;
170 
171 	if (greth->gbit_mac) {
172 
173 		/* Free and unmap RX buffers */
174 		for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
175 			if (greth->rx_skbuff[i] != NULL) {
176 				dev_kfree_skb(greth->rx_skbuff[i]);
177 				dma_unmap_single(greth->dev,
178 						 greth_read_bd(&rx_bdp->addr),
179 						 MAX_FRAME_SIZE+NET_IP_ALIGN,
180 						 DMA_FROM_DEVICE);
181 			}
182 		}
183 
184 		/* TX buffers */
185 		while (greth->tx_free < GRETH_TXBD_NUM) {
186 
187 			struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
188 			int nr_frags = skb_shinfo(skb)->nr_frags;
189 			tx_bdp = greth->tx_bd_base + greth->tx_last;
190 			greth->tx_last = NEXT_TX(greth->tx_last);
191 
192 			dma_unmap_single(greth->dev,
193 					 greth_read_bd(&tx_bdp->addr),
194 					 skb_headlen(skb),
195 					 DMA_TO_DEVICE);
196 
197 			for (i = 0; i < nr_frags; i++) {
198 				skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
199 				tx_bdp = greth->tx_bd_base + greth->tx_last;
200 
201 				dma_unmap_page(greth->dev,
202 					       greth_read_bd(&tx_bdp->addr),
203 					       skb_frag_size(frag),
204 					       DMA_TO_DEVICE);
205 
206 				greth->tx_last = NEXT_TX(greth->tx_last);
207 			}
208 			greth->tx_free += nr_frags+1;
209 			dev_kfree_skb(skb);
210 		}
211 
212 
213 	} else { /* 10/100 Mbps MAC */
214 
215 		for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
216 			kfree(greth->rx_bufs[i]);
217 			dma_unmap_single(greth->dev,
218 					 greth_read_bd(&rx_bdp->addr),
219 					 MAX_FRAME_SIZE,
220 					 DMA_FROM_DEVICE);
221 		}
222 		for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
223 			kfree(greth->tx_bufs[i]);
224 			dma_unmap_single(greth->dev,
225 					 greth_read_bd(&tx_bdp->addr),
226 					 MAX_FRAME_SIZE,
227 					 DMA_TO_DEVICE);
228 		}
229 	}
230 }
231 
greth_init_rings(struct greth_private * greth)232 static int greth_init_rings(struct greth_private *greth)
233 {
234 	struct sk_buff *skb;
235 	struct greth_bd *rx_bd, *tx_bd;
236 	u32 dma_addr;
237 	int i;
238 
239 	rx_bd = greth->rx_bd_base;
240 	tx_bd = greth->tx_bd_base;
241 
242 	/* Initialize descriptor rings and buffers */
243 	if (greth->gbit_mac) {
244 
245 		for (i = 0; i < GRETH_RXBD_NUM; i++) {
246 			skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
247 			if (skb == NULL) {
248 				if (netif_msg_ifup(greth))
249 					dev_err(greth->dev, "Error allocating DMA ring.\n");
250 				goto cleanup;
251 			}
252 			skb_reserve(skb, NET_IP_ALIGN);
253 			dma_addr = dma_map_single(greth->dev,
254 						  skb->data,
255 						  MAX_FRAME_SIZE+NET_IP_ALIGN,
256 						  DMA_FROM_DEVICE);
257 
258 			if (dma_mapping_error(greth->dev, dma_addr)) {
259 				if (netif_msg_ifup(greth))
260 					dev_err(greth->dev, "Could not create initial DMA mapping\n");
261 				dev_kfree_skb(skb);
262 				goto cleanup;
263 			}
264 			greth->rx_skbuff[i] = skb;
265 			greth_write_bd(&rx_bd[i].addr, dma_addr);
266 			greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
267 		}
268 
269 	} else {
270 
271 		/* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
272 		for (i = 0; i < GRETH_RXBD_NUM; i++) {
273 
274 			greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
275 
276 			if (greth->rx_bufs[i] == NULL) {
277 				if (netif_msg_ifup(greth))
278 					dev_err(greth->dev, "Error allocating DMA ring.\n");
279 				goto cleanup;
280 			}
281 
282 			dma_addr = dma_map_single(greth->dev,
283 						  greth->rx_bufs[i],
284 						  MAX_FRAME_SIZE,
285 						  DMA_FROM_DEVICE);
286 
287 			if (dma_mapping_error(greth->dev, dma_addr)) {
288 				if (netif_msg_ifup(greth))
289 					dev_err(greth->dev, "Could not create initial DMA mapping\n");
290 				goto cleanup;
291 			}
292 			greth_write_bd(&rx_bd[i].addr, dma_addr);
293 			greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
294 		}
295 		for (i = 0; i < GRETH_TXBD_NUM; i++) {
296 
297 			greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
298 
299 			if (greth->tx_bufs[i] == NULL) {
300 				if (netif_msg_ifup(greth))
301 					dev_err(greth->dev, "Error allocating DMA ring.\n");
302 				goto cleanup;
303 			}
304 
305 			dma_addr = dma_map_single(greth->dev,
306 						  greth->tx_bufs[i],
307 						  MAX_FRAME_SIZE,
308 						  DMA_TO_DEVICE);
309 
310 			if (dma_mapping_error(greth->dev, dma_addr)) {
311 				if (netif_msg_ifup(greth))
312 					dev_err(greth->dev, "Could not create initial DMA mapping\n");
313 				goto cleanup;
314 			}
315 			greth_write_bd(&tx_bd[i].addr, dma_addr);
316 			greth_write_bd(&tx_bd[i].stat, 0);
317 		}
318 	}
319 	greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
320 		       greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
321 
322 	/* Initialize pointers. */
323 	greth->rx_cur = 0;
324 	greth->tx_next = 0;
325 	greth->tx_last = 0;
326 	greth->tx_free = GRETH_TXBD_NUM;
327 
328 	/* Initialize descriptor base address */
329 	GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
330 	GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
331 
332 	return 0;
333 
334 cleanup:
335 	greth_clean_rings(greth);
336 	return -ENOMEM;
337 }
338 
greth_open(struct net_device * dev)339 static int greth_open(struct net_device *dev)
340 {
341 	struct greth_private *greth = netdev_priv(dev);
342 	int err;
343 
344 	err = greth_init_rings(greth);
345 	if (err) {
346 		if (netif_msg_ifup(greth))
347 			dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
348 		return err;
349 	}
350 
351 	err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
352 	if (err) {
353 		if (netif_msg_ifup(greth))
354 			dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
355 		greth_clean_rings(greth);
356 		return err;
357 	}
358 
359 	if (netif_msg_ifup(greth))
360 		dev_dbg(&dev->dev, " starting queue\n");
361 	netif_start_queue(dev);
362 
363 	GRETH_REGSAVE(greth->regs->status, 0xFF);
364 
365 	napi_enable(&greth->napi);
366 
367 	greth_enable_irqs(greth);
368 	greth_enable_tx(greth);
369 	greth_enable_rx(greth);
370 	return 0;
371 
372 }
373 
greth_close(struct net_device * dev)374 static int greth_close(struct net_device *dev)
375 {
376 	struct greth_private *greth = netdev_priv(dev);
377 
378 	napi_disable(&greth->napi);
379 
380 	greth_disable_irqs(greth);
381 	greth_disable_tx(greth);
382 	greth_disable_rx(greth);
383 
384 	netif_stop_queue(dev);
385 
386 	free_irq(greth->irq, (void *) dev);
387 
388 	greth_clean_rings(greth);
389 
390 	return 0;
391 }
392 
393 static netdev_tx_t
greth_start_xmit(struct sk_buff * skb,struct net_device * dev)394 greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
395 {
396 	struct greth_private *greth = netdev_priv(dev);
397 	struct greth_bd *bdp;
398 	int err = NETDEV_TX_OK;
399 	u32 status, dma_addr, ctrl;
400 	unsigned long flags;
401 
402 	/* Clean TX Ring */
403 	greth_clean_tx(greth->netdev);
404 
405 	if (unlikely(greth->tx_free <= 0)) {
406 		spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
407 		ctrl = GRETH_REGLOAD(greth->regs->control);
408 		/* Enable TX IRQ only if not already in poll() routine */
409 		if (ctrl & GRETH_RXI)
410 			GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
411 		netif_stop_queue(dev);
412 		spin_unlock_irqrestore(&greth->devlock, flags);
413 		return NETDEV_TX_BUSY;
414 	}
415 
416 	if (netif_msg_pktdata(greth))
417 		greth_print_tx_packet(skb);
418 
419 
420 	if (unlikely(skb->len > MAX_FRAME_SIZE)) {
421 		dev->stats.tx_errors++;
422 		goto out;
423 	}
424 
425 	bdp = greth->tx_bd_base + greth->tx_next;
426 	dma_addr = greth_read_bd(&bdp->addr);
427 
428 	memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
429 
430 	dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
431 
432 	status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
433 	greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
434 
435 	/* Wrap around descriptor ring */
436 	if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
437 		status |= GRETH_BD_WR;
438 	}
439 
440 	greth->tx_next = NEXT_TX(greth->tx_next);
441 	greth->tx_free--;
442 
443 	/* Write descriptor control word and enable transmission */
444 	greth_write_bd(&bdp->stat, status);
445 	spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
446 	greth_enable_tx(greth);
447 	spin_unlock_irqrestore(&greth->devlock, flags);
448 
449 out:
450 	dev_kfree_skb(skb);
451 	return err;
452 }
453 
greth_num_free_bds(u16 tx_last,u16 tx_next)454 static inline u16 greth_num_free_bds(u16 tx_last, u16 tx_next)
455 {
456 	if (tx_next < tx_last)
457 		return (tx_last - tx_next) - 1;
458 	else
459 		return GRETH_TXBD_NUM - (tx_next - tx_last) - 1;
460 }
461 
462 static netdev_tx_t
greth_start_xmit_gbit(struct sk_buff * skb,struct net_device * dev)463 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
464 {
465 	struct greth_private *greth = netdev_priv(dev);
466 	struct greth_bd *bdp;
467 	u32 status, dma_addr;
468 	int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
469 	unsigned long flags;
470 	u16 tx_last;
471 
472 	nr_frags = skb_shinfo(skb)->nr_frags;
473 	tx_last = greth->tx_last;
474 	rmb(); /* tx_last is updated by the poll task */
475 
476 	if (greth_num_free_bds(tx_last, greth->tx_next) < nr_frags + 1) {
477 		netif_stop_queue(dev);
478 		err = NETDEV_TX_BUSY;
479 		goto out;
480 	}
481 
482 	if (netif_msg_pktdata(greth))
483 		greth_print_tx_packet(skb);
484 
485 	if (unlikely(skb->len > MAX_FRAME_SIZE)) {
486 		dev->stats.tx_errors++;
487 		goto len_error;
488 	}
489 
490 	/* Save skb pointer. */
491 	greth->tx_skbuff[greth->tx_next] = skb;
492 
493 	/* Linear buf */
494 	if (nr_frags != 0)
495 		status = GRETH_TXBD_MORE;
496 	else
497 		status = GRETH_BD_IE;
498 
499 	if (skb->ip_summed == CHECKSUM_PARTIAL)
500 		status |= GRETH_TXBD_CSALL;
501 	status |= skb_headlen(skb) & GRETH_BD_LEN;
502 	if (greth->tx_next == GRETH_TXBD_NUM_MASK)
503 		status |= GRETH_BD_WR;
504 
505 
506 	bdp = greth->tx_bd_base + greth->tx_next;
507 	greth_write_bd(&bdp->stat, status);
508 	dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
509 
510 	if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
511 		goto map_error;
512 
513 	greth_write_bd(&bdp->addr, dma_addr);
514 
515 	curr_tx = NEXT_TX(greth->tx_next);
516 
517 	/* Frags */
518 	for (i = 0; i < nr_frags; i++) {
519 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
520 		greth->tx_skbuff[curr_tx] = NULL;
521 		bdp = greth->tx_bd_base + curr_tx;
522 
523 		status = GRETH_BD_EN;
524 		if (skb->ip_summed == CHECKSUM_PARTIAL)
525 			status |= GRETH_TXBD_CSALL;
526 		status |= skb_frag_size(frag) & GRETH_BD_LEN;
527 
528 		/* Wrap around descriptor ring */
529 		if (curr_tx == GRETH_TXBD_NUM_MASK)
530 			status |= GRETH_BD_WR;
531 
532 		/* More fragments left */
533 		if (i < nr_frags - 1)
534 			status |= GRETH_TXBD_MORE;
535 		else
536 			status |= GRETH_BD_IE; /* enable IRQ on last fragment */
537 
538 		greth_write_bd(&bdp->stat, status);
539 
540 		dma_addr = skb_frag_dma_map(greth->dev, frag, 0, skb_frag_size(frag),
541 					    DMA_TO_DEVICE);
542 
543 		if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
544 			goto frag_map_error;
545 
546 		greth_write_bd(&bdp->addr, dma_addr);
547 
548 		curr_tx = NEXT_TX(curr_tx);
549 	}
550 
551 	wmb();
552 
553 	/* Enable the descriptor chain by enabling the first descriptor */
554 	bdp = greth->tx_bd_base + greth->tx_next;
555 	greth_write_bd(&bdp->stat,
556 		       greth_read_bd(&bdp->stat) | GRETH_BD_EN);
557 
558 	spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
559 	greth->tx_next = curr_tx;
560 	greth_enable_tx_and_irq(greth);
561 	spin_unlock_irqrestore(&greth->devlock, flags);
562 
563 	return NETDEV_TX_OK;
564 
565 frag_map_error:
566 	/* Unmap SKB mappings that succeeded and disable descriptor */
567 	for (i = 0; greth->tx_next + i != curr_tx; i++) {
568 		bdp = greth->tx_bd_base + greth->tx_next + i;
569 		dma_unmap_single(greth->dev,
570 				 greth_read_bd(&bdp->addr),
571 				 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
572 				 DMA_TO_DEVICE);
573 		greth_write_bd(&bdp->stat, 0);
574 	}
575 map_error:
576 	if (net_ratelimit())
577 		dev_warn(greth->dev, "Could not create TX DMA mapping\n");
578 len_error:
579 	dev_kfree_skb(skb);
580 out:
581 	return err;
582 }
583 
greth_interrupt(int irq,void * dev_id)584 static irqreturn_t greth_interrupt(int irq, void *dev_id)
585 {
586 	struct net_device *dev = dev_id;
587 	struct greth_private *greth;
588 	u32 status, ctrl;
589 	irqreturn_t retval = IRQ_NONE;
590 
591 	greth = netdev_priv(dev);
592 
593 	spin_lock(&greth->devlock);
594 
595 	/* Get the interrupt events that caused us to be here. */
596 	status = GRETH_REGLOAD(greth->regs->status);
597 
598 	/* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
599 	 * set regardless of whether IRQ is enabled or not. Especially
600 	 * important when shared IRQ.
601 	 */
602 	ctrl = GRETH_REGLOAD(greth->regs->control);
603 
604 	/* Handle rx and tx interrupts through poll */
605 	if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
606 	    ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
607 		retval = IRQ_HANDLED;
608 
609 		/* Disable interrupts and schedule poll() */
610 		greth_disable_irqs(greth);
611 		napi_schedule(&greth->napi);
612 	}
613 
614 	spin_unlock(&greth->devlock);
615 
616 	return retval;
617 }
618 
greth_clean_tx(struct net_device * dev)619 static void greth_clean_tx(struct net_device *dev)
620 {
621 	struct greth_private *greth;
622 	struct greth_bd *bdp;
623 	u32 stat;
624 
625 	greth = netdev_priv(dev);
626 
627 	while (1) {
628 		bdp = greth->tx_bd_base + greth->tx_last;
629 		GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
630 		mb();
631 		stat = greth_read_bd(&bdp->stat);
632 
633 		if (unlikely(stat & GRETH_BD_EN))
634 			break;
635 
636 		if (greth->tx_free == GRETH_TXBD_NUM)
637 			break;
638 
639 		/* Check status for errors */
640 		if (unlikely(stat & GRETH_TXBD_STATUS)) {
641 			dev->stats.tx_errors++;
642 			if (stat & GRETH_TXBD_ERR_AL)
643 				dev->stats.tx_aborted_errors++;
644 			if (stat & GRETH_TXBD_ERR_UE)
645 				dev->stats.tx_fifo_errors++;
646 		}
647 		dev->stats.tx_packets++;
648 		dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
649 		greth->tx_last = NEXT_TX(greth->tx_last);
650 		greth->tx_free++;
651 	}
652 
653 	if (greth->tx_free > 0) {
654 		netif_wake_queue(dev);
655 	}
656 }
657 
greth_update_tx_stats(struct net_device * dev,u32 stat)658 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
659 {
660 	/* Check status for errors */
661 	if (unlikely(stat & GRETH_TXBD_STATUS)) {
662 		dev->stats.tx_errors++;
663 		if (stat & GRETH_TXBD_ERR_AL)
664 			dev->stats.tx_aborted_errors++;
665 		if (stat & GRETH_TXBD_ERR_UE)
666 			dev->stats.tx_fifo_errors++;
667 		if (stat & GRETH_TXBD_ERR_LC)
668 			dev->stats.tx_aborted_errors++;
669 	}
670 	dev->stats.tx_packets++;
671 }
672 
greth_clean_tx_gbit(struct net_device * dev)673 static void greth_clean_tx_gbit(struct net_device *dev)
674 {
675 	struct greth_private *greth;
676 	struct greth_bd *bdp, *bdp_last_frag;
677 	struct sk_buff *skb = NULL;
678 	u32 stat;
679 	int nr_frags, i;
680 	u16 tx_last;
681 
682 	greth = netdev_priv(dev);
683 	tx_last = greth->tx_last;
684 
685 	while (tx_last != greth->tx_next) {
686 
687 		skb = greth->tx_skbuff[tx_last];
688 
689 		nr_frags = skb_shinfo(skb)->nr_frags;
690 
691 		/* We only clean fully completed SKBs */
692 		bdp_last_frag = greth->tx_bd_base + SKIP_TX(tx_last, nr_frags);
693 
694 		GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
695 		mb();
696 		stat = greth_read_bd(&bdp_last_frag->stat);
697 
698 		if (stat & GRETH_BD_EN)
699 			break;
700 
701 		greth->tx_skbuff[tx_last] = NULL;
702 
703 		greth_update_tx_stats(dev, stat);
704 		dev->stats.tx_bytes += skb->len;
705 
706 		bdp = greth->tx_bd_base + tx_last;
707 
708 		tx_last = NEXT_TX(tx_last);
709 
710 		dma_unmap_single(greth->dev,
711 				 greth_read_bd(&bdp->addr),
712 				 skb_headlen(skb),
713 				 DMA_TO_DEVICE);
714 
715 		for (i = 0; i < nr_frags; i++) {
716 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
717 			bdp = greth->tx_bd_base + tx_last;
718 
719 			dma_unmap_page(greth->dev,
720 				       greth_read_bd(&bdp->addr),
721 				       skb_frag_size(frag),
722 				       DMA_TO_DEVICE);
723 
724 			tx_last = NEXT_TX(tx_last);
725 		}
726 		dev_kfree_skb(skb);
727 	}
728 	if (skb) { /* skb is set only if the above while loop was entered */
729 		wmb();
730 		greth->tx_last = tx_last;
731 
732 		if (netif_queue_stopped(dev) &&
733 		    (greth_num_free_bds(tx_last, greth->tx_next) >
734 		    (MAX_SKB_FRAGS+1)))
735 			netif_wake_queue(dev);
736 	}
737 }
738 
greth_rx(struct net_device * dev,int limit)739 static int greth_rx(struct net_device *dev, int limit)
740 {
741 	struct greth_private *greth;
742 	struct greth_bd *bdp;
743 	struct sk_buff *skb;
744 	int pkt_len;
745 	int bad, count;
746 	u32 status, dma_addr;
747 	unsigned long flags;
748 
749 	greth = netdev_priv(dev);
750 
751 	for (count = 0; count < limit; ++count) {
752 
753 		bdp = greth->rx_bd_base + greth->rx_cur;
754 		GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
755 		mb();
756 		status = greth_read_bd(&bdp->stat);
757 
758 		if (unlikely(status & GRETH_BD_EN)) {
759 			break;
760 		}
761 
762 		dma_addr = greth_read_bd(&bdp->addr);
763 		bad = 0;
764 
765 		/* Check status for errors. */
766 		if (unlikely(status & GRETH_RXBD_STATUS)) {
767 			if (status & GRETH_RXBD_ERR_FT) {
768 				dev->stats.rx_length_errors++;
769 				bad = 1;
770 			}
771 			if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
772 				dev->stats.rx_frame_errors++;
773 				bad = 1;
774 			}
775 			if (status & GRETH_RXBD_ERR_CRC) {
776 				dev->stats.rx_crc_errors++;
777 				bad = 1;
778 			}
779 		}
780 		if (unlikely(bad)) {
781 			dev->stats.rx_errors++;
782 
783 		} else {
784 
785 			pkt_len = status & GRETH_BD_LEN;
786 
787 			skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
788 
789 			if (unlikely(skb == NULL)) {
790 
791 				if (net_ratelimit())
792 					dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
793 
794 				dev->stats.rx_dropped++;
795 
796 			} else {
797 				skb_reserve(skb, NET_IP_ALIGN);
798 
799 				dma_sync_single_for_cpu(greth->dev,
800 							dma_addr,
801 							pkt_len,
802 							DMA_FROM_DEVICE);
803 
804 				if (netif_msg_pktdata(greth))
805 					greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
806 
807 				skb_put_data(skb, phys_to_virt(dma_addr),
808 					     pkt_len);
809 
810 				skb->protocol = eth_type_trans(skb, dev);
811 				dev->stats.rx_bytes += pkt_len;
812 				dev->stats.rx_packets++;
813 				netif_receive_skb(skb);
814 			}
815 		}
816 
817 		status = GRETH_BD_EN | GRETH_BD_IE;
818 		if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
819 			status |= GRETH_BD_WR;
820 		}
821 
822 		wmb();
823 		greth_write_bd(&bdp->stat, status);
824 
825 		dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
826 
827 		spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
828 		greth_enable_rx(greth);
829 		spin_unlock_irqrestore(&greth->devlock, flags);
830 
831 		greth->rx_cur = NEXT_RX(greth->rx_cur);
832 	}
833 
834 	return count;
835 }
836 
hw_checksummed(u32 status)837 static inline int hw_checksummed(u32 status)
838 {
839 
840 	if (status & GRETH_RXBD_IP_FRAG)
841 		return 0;
842 
843 	if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
844 		return 0;
845 
846 	if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
847 		return 0;
848 
849 	if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
850 		return 0;
851 
852 	return 1;
853 }
854 
greth_rx_gbit(struct net_device * dev,int limit)855 static int greth_rx_gbit(struct net_device *dev, int limit)
856 {
857 	struct greth_private *greth;
858 	struct greth_bd *bdp;
859 	struct sk_buff *skb, *newskb;
860 	int pkt_len;
861 	int bad, count = 0;
862 	u32 status, dma_addr;
863 	unsigned long flags;
864 
865 	greth = netdev_priv(dev);
866 
867 	for (count = 0; count < limit; ++count) {
868 
869 		bdp = greth->rx_bd_base + greth->rx_cur;
870 		skb = greth->rx_skbuff[greth->rx_cur];
871 		GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
872 		mb();
873 		status = greth_read_bd(&bdp->stat);
874 		bad = 0;
875 
876 		if (status & GRETH_BD_EN)
877 			break;
878 
879 		/* Check status for errors. */
880 		if (unlikely(status & GRETH_RXBD_STATUS)) {
881 
882 			if (status & GRETH_RXBD_ERR_FT) {
883 				dev->stats.rx_length_errors++;
884 				bad = 1;
885 			} else if (status &
886 				   (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
887 				dev->stats.rx_frame_errors++;
888 				bad = 1;
889 			} else if (status & GRETH_RXBD_ERR_CRC) {
890 				dev->stats.rx_crc_errors++;
891 				bad = 1;
892 			}
893 		}
894 
895 		/* Allocate new skb to replace current, not needed if the
896 		 * current skb can be reused */
897 		if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
898 			skb_reserve(newskb, NET_IP_ALIGN);
899 
900 			dma_addr = dma_map_single(greth->dev,
901 						      newskb->data,
902 						      MAX_FRAME_SIZE + NET_IP_ALIGN,
903 						      DMA_FROM_DEVICE);
904 
905 			if (!dma_mapping_error(greth->dev, dma_addr)) {
906 				/* Process the incoming frame. */
907 				pkt_len = status & GRETH_BD_LEN;
908 
909 				dma_unmap_single(greth->dev,
910 						 greth_read_bd(&bdp->addr),
911 						 MAX_FRAME_SIZE + NET_IP_ALIGN,
912 						 DMA_FROM_DEVICE);
913 
914 				if (netif_msg_pktdata(greth))
915 					greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
916 
917 				skb_put(skb, pkt_len);
918 
919 				if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
920 					skb->ip_summed = CHECKSUM_UNNECESSARY;
921 				else
922 					skb_checksum_none_assert(skb);
923 
924 				skb->protocol = eth_type_trans(skb, dev);
925 				dev->stats.rx_packets++;
926 				dev->stats.rx_bytes += pkt_len;
927 				netif_receive_skb(skb);
928 
929 				greth->rx_skbuff[greth->rx_cur] = newskb;
930 				greth_write_bd(&bdp->addr, dma_addr);
931 			} else {
932 				if (net_ratelimit())
933 					dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
934 				dev_kfree_skb(newskb);
935 				/* reusing current skb, so it is a drop */
936 				dev->stats.rx_dropped++;
937 			}
938 		} else if (bad) {
939 			/* Bad Frame transfer, the skb is reused */
940 			dev->stats.rx_dropped++;
941 		} else {
942 			/* Failed Allocating a new skb. This is rather stupid
943 			 * but the current "filled" skb is reused, as if
944 			 * transfer failure. One could argue that RX descriptor
945 			 * table handling should be divided into cleaning and
946 			 * filling as the TX part of the driver
947 			 */
948 			if (net_ratelimit())
949 				dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
950 			/* reusing current skb, so it is a drop */
951 			dev->stats.rx_dropped++;
952 		}
953 
954 		status = GRETH_BD_EN | GRETH_BD_IE;
955 		if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
956 			status |= GRETH_BD_WR;
957 		}
958 
959 		wmb();
960 		greth_write_bd(&bdp->stat, status);
961 		spin_lock_irqsave(&greth->devlock, flags);
962 		greth_enable_rx(greth);
963 		spin_unlock_irqrestore(&greth->devlock, flags);
964 		greth->rx_cur = NEXT_RX(greth->rx_cur);
965 	}
966 
967 	return count;
968 
969 }
970 
greth_poll(struct napi_struct * napi,int budget)971 static int greth_poll(struct napi_struct *napi, int budget)
972 {
973 	struct greth_private *greth;
974 	int work_done = 0;
975 	unsigned long flags;
976 	u32 mask, ctrl;
977 	greth = container_of(napi, struct greth_private, napi);
978 
979 restart_txrx_poll:
980 	if (greth->gbit_mac) {
981 		greth_clean_tx_gbit(greth->netdev);
982 		work_done += greth_rx_gbit(greth->netdev, budget - work_done);
983 	} else {
984 		if (netif_queue_stopped(greth->netdev))
985 			greth_clean_tx(greth->netdev);
986 		work_done += greth_rx(greth->netdev, budget - work_done);
987 	}
988 
989 	if (work_done < budget) {
990 
991 		spin_lock_irqsave(&greth->devlock, flags);
992 
993 		ctrl = GRETH_REGLOAD(greth->regs->control);
994 		if ((greth->gbit_mac && (greth->tx_last != greth->tx_next)) ||
995 		    (!greth->gbit_mac && netif_queue_stopped(greth->netdev))) {
996 			GRETH_REGSAVE(greth->regs->control,
997 					ctrl | GRETH_TXI | GRETH_RXI);
998 			mask = GRETH_INT_RX | GRETH_INT_RE |
999 			       GRETH_INT_TX | GRETH_INT_TE;
1000 		} else {
1001 			GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
1002 			mask = GRETH_INT_RX | GRETH_INT_RE;
1003 		}
1004 
1005 		if (GRETH_REGLOAD(greth->regs->status) & mask) {
1006 			GRETH_REGSAVE(greth->regs->control, ctrl);
1007 			spin_unlock_irqrestore(&greth->devlock, flags);
1008 			goto restart_txrx_poll;
1009 		} else {
1010 			napi_complete_done(napi, work_done);
1011 			spin_unlock_irqrestore(&greth->devlock, flags);
1012 		}
1013 	}
1014 
1015 	return work_done;
1016 }
1017 
greth_set_mac_add(struct net_device * dev,void * p)1018 static int greth_set_mac_add(struct net_device *dev, void *p)
1019 {
1020 	struct sockaddr *addr = p;
1021 	struct greth_private *greth;
1022 	struct greth_regs *regs;
1023 
1024 	greth = netdev_priv(dev);
1025 	regs = greth->regs;
1026 
1027 	if (!is_valid_ether_addr(addr->sa_data))
1028 		return -EADDRNOTAVAIL;
1029 
1030 	eth_hw_addr_set(dev, addr->sa_data);
1031 	GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1032 	GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1033 		      dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1034 
1035 	return 0;
1036 }
1037 
greth_hash_get_index(__u8 * addr)1038 static u32 greth_hash_get_index(__u8 *addr)
1039 {
1040 	return (ether_crc(6, addr)) & 0x3F;
1041 }
1042 
greth_set_hash_filter(struct net_device * dev)1043 static void greth_set_hash_filter(struct net_device *dev)
1044 {
1045 	struct netdev_hw_addr *ha;
1046 	struct greth_private *greth = netdev_priv(dev);
1047 	struct greth_regs *regs = greth->regs;
1048 	u32 mc_filter[2];
1049 	unsigned int bitnr;
1050 
1051 	mc_filter[0] = mc_filter[1] = 0;
1052 
1053 	netdev_for_each_mc_addr(ha, dev) {
1054 		bitnr = greth_hash_get_index(ha->addr);
1055 		mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1056 	}
1057 
1058 	GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1059 	GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1060 }
1061 
greth_set_multicast_list(struct net_device * dev)1062 static void greth_set_multicast_list(struct net_device *dev)
1063 {
1064 	int cfg;
1065 	struct greth_private *greth = netdev_priv(dev);
1066 	struct greth_regs *regs = greth->regs;
1067 
1068 	cfg = GRETH_REGLOAD(regs->control);
1069 	if (dev->flags & IFF_PROMISC)
1070 		cfg |= GRETH_CTRL_PR;
1071 	else
1072 		cfg &= ~GRETH_CTRL_PR;
1073 
1074 	if (greth->multicast) {
1075 		if (dev->flags & IFF_ALLMULTI) {
1076 			GRETH_REGSAVE(regs->hash_msb, -1);
1077 			GRETH_REGSAVE(regs->hash_lsb, -1);
1078 			cfg |= GRETH_CTRL_MCEN;
1079 			GRETH_REGSAVE(regs->control, cfg);
1080 			return;
1081 		}
1082 
1083 		if (netdev_mc_empty(dev)) {
1084 			cfg &= ~GRETH_CTRL_MCEN;
1085 			GRETH_REGSAVE(regs->control, cfg);
1086 			return;
1087 		}
1088 
1089 		/* Setup multicast filter */
1090 		greth_set_hash_filter(dev);
1091 		cfg |= GRETH_CTRL_MCEN;
1092 	}
1093 	GRETH_REGSAVE(regs->control, cfg);
1094 }
1095 
greth_get_msglevel(struct net_device * dev)1096 static u32 greth_get_msglevel(struct net_device *dev)
1097 {
1098 	struct greth_private *greth = netdev_priv(dev);
1099 	return greth->msg_enable;
1100 }
1101 
greth_set_msglevel(struct net_device * dev,u32 value)1102 static void greth_set_msglevel(struct net_device *dev, u32 value)
1103 {
1104 	struct greth_private *greth = netdev_priv(dev);
1105 	greth->msg_enable = value;
1106 }
1107 
greth_get_regs_len(struct net_device * dev)1108 static int greth_get_regs_len(struct net_device *dev)
1109 {
1110 	return sizeof(struct greth_regs);
1111 }
1112 
greth_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1113 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1114 {
1115 	struct greth_private *greth = netdev_priv(dev);
1116 
1117 	strscpy(info->driver, dev_driver_string(greth->dev),
1118 		sizeof(info->driver));
1119 	strscpy(info->bus_info, greth->dev->bus->name, sizeof(info->bus_info));
1120 }
1121 
greth_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * p)1122 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1123 {
1124 	int i;
1125 	struct greth_private *greth = netdev_priv(dev);
1126 	u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1127 	u32 *buff = p;
1128 
1129 	for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1130 		buff[i] = greth_read_bd(&greth_regs[i]);
1131 }
1132 
1133 static const struct ethtool_ops greth_ethtool_ops = {
1134 	.get_msglevel		= greth_get_msglevel,
1135 	.set_msglevel		= greth_set_msglevel,
1136 	.get_drvinfo		= greth_get_drvinfo,
1137 	.get_regs_len           = greth_get_regs_len,
1138 	.get_regs               = greth_get_regs,
1139 	.get_link		= ethtool_op_get_link,
1140 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1141 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1142 };
1143 
1144 static struct net_device_ops greth_netdev_ops = {
1145 	.ndo_open		= greth_open,
1146 	.ndo_stop		= greth_close,
1147 	.ndo_start_xmit		= greth_start_xmit,
1148 	.ndo_set_mac_address	= greth_set_mac_add,
1149 	.ndo_validate_addr	= eth_validate_addr,
1150 };
1151 
wait_for_mdio(struct greth_private * greth)1152 static inline int wait_for_mdio(struct greth_private *greth)
1153 {
1154 	unsigned long timeout = jiffies + 4*HZ/100;
1155 	while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1156 		if (time_after(jiffies, timeout))
1157 			return 0;
1158 	}
1159 	return 1;
1160 }
1161 
greth_mdio_read(struct mii_bus * bus,int phy,int reg)1162 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1163 {
1164 	struct greth_private *greth = bus->priv;
1165 	int data;
1166 
1167 	if (!wait_for_mdio(greth))
1168 		return -EBUSY;
1169 
1170 	GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1171 
1172 	if (!wait_for_mdio(greth))
1173 		return -EBUSY;
1174 
1175 	if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1176 		data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1177 		return data;
1178 
1179 	} else {
1180 		return -1;
1181 	}
1182 }
1183 
greth_mdio_write(struct mii_bus * bus,int phy,int reg,u16 val)1184 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1185 {
1186 	struct greth_private *greth = bus->priv;
1187 
1188 	if (!wait_for_mdio(greth))
1189 		return -EBUSY;
1190 
1191 	GRETH_REGSAVE(greth->regs->mdio,
1192 		      ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1193 
1194 	if (!wait_for_mdio(greth))
1195 		return -EBUSY;
1196 
1197 	return 0;
1198 }
1199 
greth_link_change(struct net_device * dev)1200 static void greth_link_change(struct net_device *dev)
1201 {
1202 	struct greth_private *greth = netdev_priv(dev);
1203 	struct phy_device *phydev = dev->phydev;
1204 	unsigned long flags;
1205 	int status_change = 0;
1206 	u32 ctrl;
1207 
1208 	spin_lock_irqsave(&greth->devlock, flags);
1209 
1210 	if (phydev->link) {
1211 
1212 		if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1213 			ctrl = GRETH_REGLOAD(greth->regs->control) &
1214 			       ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1215 
1216 			if (phydev->duplex)
1217 				ctrl |= GRETH_CTRL_FD;
1218 
1219 			if (phydev->speed == SPEED_100)
1220 				ctrl |= GRETH_CTRL_SP;
1221 			else if (phydev->speed == SPEED_1000)
1222 				ctrl |= GRETH_CTRL_GB;
1223 
1224 			GRETH_REGSAVE(greth->regs->control, ctrl);
1225 			greth->speed = phydev->speed;
1226 			greth->duplex = phydev->duplex;
1227 			status_change = 1;
1228 		}
1229 	}
1230 
1231 	if (phydev->link != greth->link) {
1232 		if (!phydev->link) {
1233 			greth->speed = 0;
1234 			greth->duplex = -1;
1235 		}
1236 		greth->link = phydev->link;
1237 
1238 		status_change = 1;
1239 	}
1240 
1241 	spin_unlock_irqrestore(&greth->devlock, flags);
1242 
1243 	if (status_change) {
1244 		if (phydev->link)
1245 			pr_debug("%s: link up (%d/%s)\n",
1246 				dev->name, phydev->speed,
1247 				DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1248 		else
1249 			pr_debug("%s: link down\n", dev->name);
1250 	}
1251 }
1252 
greth_mdio_probe(struct net_device * dev)1253 static int greth_mdio_probe(struct net_device *dev)
1254 {
1255 	struct greth_private *greth = netdev_priv(dev);
1256 	struct phy_device *phy = NULL;
1257 	int ret;
1258 
1259 	/* Find the first PHY */
1260 	phy = phy_find_first(greth->mdio);
1261 
1262 	if (!phy) {
1263 		if (netif_msg_probe(greth))
1264 			dev_err(&dev->dev, "no PHY found\n");
1265 		return -ENXIO;
1266 	}
1267 
1268 	ret = phy_connect_direct(dev, phy, &greth_link_change,
1269 				 greth->gbit_mac ? PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII);
1270 	if (ret) {
1271 		if (netif_msg_ifup(greth))
1272 			dev_err(&dev->dev, "could not attach to PHY\n");
1273 		return ret;
1274 	}
1275 
1276 	if (greth->gbit_mac)
1277 		phy_set_max_speed(phy, SPEED_1000);
1278 	else
1279 		phy_set_max_speed(phy, SPEED_100);
1280 
1281 	linkmode_copy(phy->advertising, phy->supported);
1282 
1283 	greth->link = 0;
1284 	greth->speed = 0;
1285 	greth->duplex = -1;
1286 
1287 	return 0;
1288 }
1289 
greth_mdio_init(struct greth_private * greth)1290 static int greth_mdio_init(struct greth_private *greth)
1291 {
1292 	int ret;
1293 	unsigned long timeout;
1294 	struct net_device *ndev = greth->netdev;
1295 
1296 	greth->mdio = mdiobus_alloc();
1297 	if (!greth->mdio) {
1298 		return -ENOMEM;
1299 	}
1300 
1301 	greth->mdio->name = "greth-mdio";
1302 	snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1303 	greth->mdio->read = greth_mdio_read;
1304 	greth->mdio->write = greth_mdio_write;
1305 	greth->mdio->priv = greth;
1306 
1307 	ret = mdiobus_register(greth->mdio);
1308 	if (ret) {
1309 		goto error;
1310 	}
1311 
1312 	ret = greth_mdio_probe(greth->netdev);
1313 	if (ret) {
1314 		if (netif_msg_probe(greth))
1315 			dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1316 		goto unreg_mdio;
1317 	}
1318 
1319 	phy_start(ndev->phydev);
1320 
1321 	/* If Ethernet debug link is used make autoneg happen right away */
1322 	if (greth->edcl && greth_edcl == 1) {
1323 		phy_start_aneg(ndev->phydev);
1324 		timeout = jiffies + 6*HZ;
1325 		while (!phy_aneg_done(ndev->phydev) &&
1326 		       time_before(jiffies, timeout)) {
1327 		}
1328 		phy_read_status(ndev->phydev);
1329 		greth_link_change(greth->netdev);
1330 	}
1331 
1332 	return 0;
1333 
1334 unreg_mdio:
1335 	mdiobus_unregister(greth->mdio);
1336 error:
1337 	mdiobus_free(greth->mdio);
1338 	return ret;
1339 }
1340 
1341 /* Initialize the GRETH MAC */
greth_of_probe(struct platform_device * ofdev)1342 static int greth_of_probe(struct platform_device *ofdev)
1343 {
1344 	struct net_device *dev;
1345 	struct greth_private *greth;
1346 	struct greth_regs *regs;
1347 
1348 	int i;
1349 	int err;
1350 	int tmp;
1351 	u8 addr[ETH_ALEN];
1352 	unsigned long timeout;
1353 
1354 	dev = alloc_etherdev(sizeof(struct greth_private));
1355 
1356 	if (dev == NULL)
1357 		return -ENOMEM;
1358 
1359 	greth = netdev_priv(dev);
1360 	greth->netdev = dev;
1361 	greth->dev = &ofdev->dev;
1362 
1363 	if (greth_debug > 0)
1364 		greth->msg_enable = greth_debug;
1365 	else
1366 		greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1367 
1368 	spin_lock_init(&greth->devlock);
1369 
1370 	greth->regs = of_ioremap(&ofdev->resource[0], 0,
1371 				 resource_size(&ofdev->resource[0]),
1372 				 "grlib-greth regs");
1373 
1374 	if (greth->regs == NULL) {
1375 		if (netif_msg_probe(greth))
1376 			dev_err(greth->dev, "ioremap failure.\n");
1377 		err = -EIO;
1378 		goto error1;
1379 	}
1380 
1381 	regs = greth->regs;
1382 	greth->irq = ofdev->archdata.irqs[0];
1383 
1384 	dev_set_drvdata(greth->dev, dev);
1385 	SET_NETDEV_DEV(dev, greth->dev);
1386 
1387 	if (netif_msg_probe(greth))
1388 		dev_dbg(greth->dev, "resetting controller.\n");
1389 
1390 	/* Reset the controller. */
1391 	GRETH_REGSAVE(regs->control, GRETH_RESET);
1392 
1393 	/* Wait for MAC to reset itself */
1394 	timeout = jiffies + HZ/100;
1395 	while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1396 		if (time_after(jiffies, timeout)) {
1397 			err = -EIO;
1398 			if (netif_msg_probe(greth))
1399 				dev_err(greth->dev, "timeout when waiting for reset.\n");
1400 			goto error2;
1401 		}
1402 	}
1403 
1404 	/* Get default PHY address  */
1405 	greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1406 
1407 	/* Check if we have GBIT capable MAC */
1408 	tmp = GRETH_REGLOAD(regs->control);
1409 	greth->gbit_mac = (tmp >> 27) & 1;
1410 
1411 	/* Check for multicast capability */
1412 	greth->multicast = (tmp >> 25) & 1;
1413 
1414 	greth->edcl = (tmp >> 31) & 1;
1415 
1416 	/* If we have EDCL we disable the EDCL speed-duplex FSM so
1417 	 * it doesn't interfere with the software */
1418 	if (greth->edcl != 0)
1419 		GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1420 
1421 	/* Check if MAC can handle MDIO interrupts */
1422 	greth->mdio_int_en = (tmp >> 26) & 1;
1423 
1424 	err = greth_mdio_init(greth);
1425 	if (err) {
1426 		if (netif_msg_probe(greth))
1427 			dev_err(greth->dev, "failed to register MDIO bus\n");
1428 		goto error2;
1429 	}
1430 
1431 	/* Allocate TX descriptor ring in coherent memory */
1432 	greth->tx_bd_base = dma_alloc_coherent(greth->dev, 1024,
1433 					       &greth->tx_bd_base_phys,
1434 					       GFP_KERNEL);
1435 	if (!greth->tx_bd_base) {
1436 		err = -ENOMEM;
1437 		goto error3;
1438 	}
1439 
1440 	/* Allocate RX descriptor ring in coherent memory */
1441 	greth->rx_bd_base = dma_alloc_coherent(greth->dev, 1024,
1442 					       &greth->rx_bd_base_phys,
1443 					       GFP_KERNEL);
1444 	if (!greth->rx_bd_base) {
1445 		err = -ENOMEM;
1446 		goto error4;
1447 	}
1448 
1449 	/* Get MAC address from: module param, OF property or ID prom */
1450 	for (i = 0; i < 6; i++) {
1451 		if (macaddr[i] != 0)
1452 			break;
1453 	}
1454 	if (i == 6) {
1455 		err = of_get_mac_address(ofdev->dev.of_node, addr);
1456 		if (!err) {
1457 			for (i = 0; i < 6; i++)
1458 				macaddr[i] = (unsigned int) addr[i];
1459 		} else {
1460 #ifdef CONFIG_SPARC
1461 			for (i = 0; i < 6; i++)
1462 				macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1463 #endif
1464 		}
1465 	}
1466 
1467 	for (i = 0; i < 6; i++)
1468 		addr[i] = macaddr[i];
1469 	eth_hw_addr_set(dev, addr);
1470 
1471 	macaddr[5]++;
1472 
1473 	if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1474 		if (netif_msg_probe(greth))
1475 			dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1476 		err = -EINVAL;
1477 		goto error5;
1478 	}
1479 
1480 	GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1481 	GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1482 		      dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1483 
1484 	/* Clear all pending interrupts except PHY irq */
1485 	GRETH_REGSAVE(regs->status, 0xFF);
1486 
1487 	if (greth->gbit_mac) {
1488 		dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1489 			NETIF_F_RXCSUM;
1490 		dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1491 		greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1492 	}
1493 
1494 	if (greth->multicast) {
1495 		greth_netdev_ops.ndo_set_rx_mode = greth_set_multicast_list;
1496 		dev->flags |= IFF_MULTICAST;
1497 	} else {
1498 		dev->flags &= ~IFF_MULTICAST;
1499 	}
1500 
1501 	dev->netdev_ops = &greth_netdev_ops;
1502 	dev->ethtool_ops = &greth_ethtool_ops;
1503 
1504 	err = register_netdev(dev);
1505 	if (err) {
1506 		if (netif_msg_probe(greth))
1507 			dev_err(greth->dev, "netdevice registration failed.\n");
1508 		goto error5;
1509 	}
1510 
1511 	/* setup NAPI */
1512 	netif_napi_add(dev, &greth->napi, greth_poll);
1513 
1514 	return 0;
1515 
1516 error5:
1517 	dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1518 error4:
1519 	dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1520 error3:
1521 	mdiobus_unregister(greth->mdio);
1522 error2:
1523 	of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1524 error1:
1525 	free_netdev(dev);
1526 	return err;
1527 }
1528 
greth_of_remove(struct platform_device * of_dev)1529 static void greth_of_remove(struct platform_device *of_dev)
1530 {
1531 	struct net_device *ndev = platform_get_drvdata(of_dev);
1532 	struct greth_private *greth = netdev_priv(ndev);
1533 
1534 	/* Free descriptor areas */
1535 	dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1536 
1537 	dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1538 
1539 	if (ndev->phydev)
1540 		phy_stop(ndev->phydev);
1541 	mdiobus_unregister(greth->mdio);
1542 
1543 	unregister_netdev(ndev);
1544 
1545 	of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1546 
1547 	free_netdev(ndev);
1548 }
1549 
1550 static const struct of_device_id greth_of_match[] = {
1551 	{
1552 	 .name = "GAISLER_ETHMAC",
1553 	 },
1554 	{
1555 	 .name = "01_01d",
1556 	 },
1557 	{},
1558 };
1559 
1560 MODULE_DEVICE_TABLE(of, greth_of_match);
1561 
1562 static struct platform_driver greth_of_driver = {
1563 	.driver = {
1564 		.name = "grlib-greth",
1565 		.of_match_table = greth_of_match,
1566 	},
1567 	.probe = greth_of_probe,
1568 	.remove_new = greth_of_remove,
1569 };
1570 
1571 module_platform_driver(greth_of_driver);
1572 
1573 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1574 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1575 MODULE_LICENSE("GPL");
1576