1 // SPDX-License-Identifier: GPL-2.0
2 /* Driver for SGI's IOC3 based Ethernet cards as found in the PCI card.
3  *
4  * Copyright (C) 1999, 2000, 01, 03, 06 Ralf Baechle
5  * Copyright (C) 1995, 1999, 2000, 2001 by Silicon Graphics, Inc.
6  *
7  * References:
8  *  o IOC3 ASIC specification 4.51, 1996-04-18
9  *  o IEEE 802.3 specification, 2000 edition
10  *  o DP38840A Specification, National Semiconductor, March 1997
11  *
12  * To do:
13  *
14  *  o Use prefetching for large packets.  What is a good lower limit for
15  *    prefetching?
16  *  o Use hardware checksums.
17  *  o Which PHYs might possibly be attached to the IOC3 in real live,
18  *    which workarounds are required for them?  Do we ever have Lucent's?
19  *  o For the 2.5 branch kill the mii-tool ioctls.
20  */
21 
22 #define IOC3_NAME	"ioc3-eth"
23 #define IOC3_VERSION	"2.6.3-4"
24 
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/errno.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/crc16.h>
32 #include <linux/crc32.h>
33 #include <linux/mii.h>
34 #include <linux/in.h>
35 #include <linux/io.h>
36 #include <linux/ip.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/gfp.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/skbuff.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/platform_device.h>
46 #include <linux/nvmem-consumer.h>
47 
48 #include <net/ip.h>
49 
50 #include <asm/sn/ioc3.h>
51 #include <asm/pci/bridge.h>
52 
53 #define CRC16_INIT	0
54 #define CRC16_VALID	0xb001
55 
56 /* Number of RX buffers.  This is tunable in the range of 16 <= x < 512.
57  * The value must be a power of two.
58  */
59 #define RX_BUFFS		64
60 #define RX_RING_ENTRIES		512		/* fixed in hardware */
61 #define RX_RING_MASK		(RX_RING_ENTRIES - 1)
62 #define RX_RING_SIZE		(RX_RING_ENTRIES * sizeof(u64))
63 
64 /* 128 TX buffers (not tunable) */
65 #define TX_RING_ENTRIES		128
66 #define TX_RING_MASK		(TX_RING_ENTRIES - 1)
67 #define TX_RING_SIZE		(TX_RING_ENTRIES * sizeof(struct ioc3_etxd))
68 
69 /* IOC3 does dma transfers in 128 byte blocks */
70 #define IOC3_DMA_XFER_LEN	128UL
71 
72 /* Every RX buffer starts with 8 byte descriptor data */
73 #define RX_OFFSET		(sizeof(struct ioc3_erxbuf) + NET_IP_ALIGN)
74 #define RX_BUF_SIZE		(13 * IOC3_DMA_XFER_LEN)
75 
76 #define ETCSR_FD   ((21 << ETCSR_IPGR2_SHIFT) | (21 << ETCSR_IPGR1_SHIFT) | 21)
77 #define ETCSR_HD   ((17 << ETCSR_IPGR2_SHIFT) | (11 << ETCSR_IPGR1_SHIFT) | 21)
78 
79 /* Private per NIC data of the driver.  */
80 struct ioc3_private {
81 	struct ioc3_ethregs *regs;
82 	struct device *dma_dev;
83 	u32 *ssram;
84 	unsigned long *rxr;		/* pointer to receiver ring */
85 	void *tx_ring;
86 	struct ioc3_etxd *txr;
87 	dma_addr_t rxr_dma;
88 	dma_addr_t txr_dma;
89 	struct sk_buff *rx_skbs[RX_RING_ENTRIES];
90 	struct sk_buff *tx_skbs[TX_RING_ENTRIES];
91 	int rx_ci;			/* RX consumer index */
92 	int rx_pi;			/* RX producer index */
93 	int tx_ci;			/* TX consumer index */
94 	int tx_pi;			/* TX producer index */
95 	int txqlen;
96 	u32 emcr, ehar_h, ehar_l;
97 	spinlock_t ioc3_lock;
98 	struct mii_if_info mii;
99 
100 	/* Members used by autonegotiation  */
101 	struct timer_list ioc3_timer;
102 };
103 
104 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
105 static void ioc3_set_multicast_list(struct net_device *dev);
106 static netdev_tx_t ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
107 static void ioc3_timeout(struct net_device *dev, unsigned int txqueue);
108 static inline unsigned int ioc3_hash(const unsigned char *addr);
109 static void ioc3_start(struct ioc3_private *ip);
110 static inline void ioc3_stop(struct ioc3_private *ip);
111 static void ioc3_init(struct net_device *dev);
112 static int ioc3_alloc_rx_bufs(struct net_device *dev);
113 static void ioc3_free_rx_bufs(struct ioc3_private *ip);
114 static inline void ioc3_clean_tx_ring(struct ioc3_private *ip);
115 
116 static const struct ethtool_ops ioc3_ethtool_ops;
117 
118 static inline unsigned long aligned_rx_skb_addr(unsigned long addr)
119 {
120 	return (~addr + 1) & (IOC3_DMA_XFER_LEN - 1UL);
121 }
122 
123 static inline int ioc3_alloc_skb(struct ioc3_private *ip, struct sk_buff **skb,
124 				 struct ioc3_erxbuf **rxb, dma_addr_t *rxb_dma)
125 {
126 	struct sk_buff *new_skb;
127 	dma_addr_t d;
128 	int offset;
129 
130 	new_skb = alloc_skb(RX_BUF_SIZE + IOC3_DMA_XFER_LEN - 1, GFP_ATOMIC);
131 	if (!new_skb)
132 		return -ENOMEM;
133 
134 	/* ensure buffer is aligned to IOC3_DMA_XFER_LEN */
135 	offset = aligned_rx_skb_addr((unsigned long)new_skb->data);
136 	if (offset)
137 		skb_reserve(new_skb, offset);
138 
139 	d = dma_map_single(ip->dma_dev, new_skb->data,
140 			   RX_BUF_SIZE, DMA_FROM_DEVICE);
141 
142 	if (dma_mapping_error(ip->dma_dev, d)) {
143 		dev_kfree_skb_any(new_skb);
144 		return -ENOMEM;
145 	}
146 	*rxb_dma = d;
147 	*rxb = (struct ioc3_erxbuf *)new_skb->data;
148 	skb_reserve(new_skb, RX_OFFSET);
149 	*skb = new_skb;
150 
151 	return 0;
152 }
153 
154 #ifdef CONFIG_PCI_XTALK_BRIDGE
155 static inline unsigned long ioc3_map(dma_addr_t addr, unsigned long attr)
156 {
157 	return (addr & ~PCI64_ATTR_BAR) | attr;
158 }
159 
160 #define ERBAR_VAL	(ERBAR_BARRIER_BIT << ERBAR_RXBARR_SHIFT)
161 #else
162 static inline unsigned long ioc3_map(dma_addr_t addr, unsigned long attr)
163 {
164 	return addr;
165 }
166 
167 #define ERBAR_VAL	0
168 #endif
169 
170 static int ioc3eth_nvmem_match(struct device *dev, const void *data)
171 {
172 	const char *name = dev_name(dev);
173 	const char *prefix = data;
174 	int prefix_len;
175 
176 	prefix_len = strlen(prefix);
177 	if (strlen(name) < (prefix_len + 3))
178 		return 0;
179 
180 	if (memcmp(prefix, name, prefix_len) != 0)
181 		return 0;
182 
183 	/* found nvmem device which is attached to our ioc3
184 	 * now check for one wire family code 09, 89 and 91
185 	 */
186 	if (memcmp(name + prefix_len, "09-", 3) == 0)
187 		return 1;
188 	if (memcmp(name + prefix_len, "89-", 3) == 0)
189 		return 1;
190 	if (memcmp(name + prefix_len, "91-", 3) == 0)
191 		return 1;
192 
193 	return 0;
194 }
195 
196 static int ioc3eth_get_mac_addr(struct resource *res, u8 mac_addr[6])
197 {
198 	struct nvmem_device *nvmem;
199 	char prefix[24];
200 	u8 prom[16];
201 	int ret;
202 	int i;
203 
204 	snprintf(prefix, sizeof(prefix), "ioc3-%012llx-",
205 		 res->start & ~0xffff);
206 
207 	nvmem = nvmem_device_find(prefix, ioc3eth_nvmem_match);
208 	if (IS_ERR(nvmem))
209 		return PTR_ERR(nvmem);
210 
211 	ret = nvmem_device_read(nvmem, 0, 16, prom);
212 	nvmem_device_put(nvmem);
213 	if (ret < 0)
214 		return ret;
215 
216 	/* check, if content is valid */
217 	if (prom[0] != 0x0a ||
218 	    crc16(CRC16_INIT, prom, 13) != CRC16_VALID)
219 		return -EINVAL;
220 
221 	for (i = 0; i < 6; i++)
222 		mac_addr[i] = prom[10 - i];
223 
224 	return 0;
225 }
226 
227 static void __ioc3_set_mac_address(struct net_device *dev)
228 {
229 	struct ioc3_private *ip = netdev_priv(dev);
230 
231 	writel((dev->dev_addr[5] <<  8) |
232 	       dev->dev_addr[4],
233 	       &ip->regs->emar_h);
234 	writel((dev->dev_addr[3] << 24) |
235 	       (dev->dev_addr[2] << 16) |
236 	       (dev->dev_addr[1] <<  8) |
237 	       dev->dev_addr[0],
238 	       &ip->regs->emar_l);
239 }
240 
241 static int ioc3_set_mac_address(struct net_device *dev, void *addr)
242 {
243 	struct ioc3_private *ip = netdev_priv(dev);
244 	struct sockaddr *sa = addr;
245 
246 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
247 
248 	spin_lock_irq(&ip->ioc3_lock);
249 	__ioc3_set_mac_address(dev);
250 	spin_unlock_irq(&ip->ioc3_lock);
251 
252 	return 0;
253 }
254 
255 /* Caller must hold the ioc3_lock ever for MII readers.  This is also
256  * used to protect the transmitter side but it's low contention.
257  */
258 static int ioc3_mdio_read(struct net_device *dev, int phy, int reg)
259 {
260 	struct ioc3_private *ip = netdev_priv(dev);
261 	struct ioc3_ethregs *regs = ip->regs;
262 
263 	while (readl(&regs->micr) & MICR_BUSY)
264 		;
265 	writel((phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG,
266 	       &regs->micr);
267 	while (readl(&regs->micr) & MICR_BUSY)
268 		;
269 
270 	return readl(&regs->midr_r) & MIDR_DATA_MASK;
271 }
272 
273 static void ioc3_mdio_write(struct net_device *dev, int phy, int reg, int data)
274 {
275 	struct ioc3_private *ip = netdev_priv(dev);
276 	struct ioc3_ethregs *regs = ip->regs;
277 
278 	while (readl(&regs->micr) & MICR_BUSY)
279 		;
280 	writel(data, &regs->midr_w);
281 	writel((phy << MICR_PHYADDR_SHIFT) | reg, &regs->micr);
282 	while (readl(&regs->micr) & MICR_BUSY)
283 		;
284 }
285 
286 static int ioc3_mii_init(struct ioc3_private *ip);
287 
288 static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
289 {
290 	struct ioc3_private *ip = netdev_priv(dev);
291 	struct ioc3_ethregs *regs = ip->regs;
292 
293 	dev->stats.collisions += readl(&regs->etcdc) & ETCDC_COLLCNT_MASK;
294 	return &dev->stats;
295 }
296 
297 static void ioc3_tcpudp_checksum(struct sk_buff *skb, u32 hwsum, int len)
298 {
299 	struct ethhdr *eh = eth_hdr(skb);
300 	unsigned int proto;
301 	unsigned char *cp;
302 	struct iphdr *ih;
303 	u32 csum, ehsum;
304 	u16 *ew;
305 
306 	/* Did hardware handle the checksum at all?  The cases we can handle
307 	 * are:
308 	 *
309 	 * - TCP and UDP checksums of IPv4 only.
310 	 * - IPv6 would be doable but we keep that for later ...
311 	 * - Only unfragmented packets.  Did somebody already tell you
312 	 *   fragmentation is evil?
313 	 * - don't care about packet size.  Worst case when processing a
314 	 *   malformed packet we'll try to access the packet at ip header +
315 	 *   64 bytes which is still inside the skb.  Even in the unlikely
316 	 *   case where the checksum is right the higher layers will still
317 	 *   drop the packet as appropriate.
318 	 */
319 	if (eh->h_proto != htons(ETH_P_IP))
320 		return;
321 
322 	ih = (struct iphdr *)((char *)eh + ETH_HLEN);
323 	if (ip_is_fragment(ih))
324 		return;
325 
326 	proto = ih->protocol;
327 	if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
328 		return;
329 
330 	/* Same as tx - compute csum of pseudo header  */
331 	csum = hwsum +
332 	       (ih->tot_len - (ih->ihl << 2)) +
333 	       htons((u16)ih->protocol) +
334 	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
335 	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
336 
337 	/* Sum up ethernet dest addr, src addr and protocol  */
338 	ew = (u16 *)eh;
339 	ehsum = ew[0] + ew[1] + ew[2] + ew[3] + ew[4] + ew[5] + ew[6];
340 
341 	ehsum = (ehsum & 0xffff) + (ehsum >> 16);
342 	ehsum = (ehsum & 0xffff) + (ehsum >> 16);
343 
344 	csum += 0xffff ^ ehsum;
345 
346 	/* In the next step we also subtract the 1's complement
347 	 * checksum of the trailing ethernet CRC.
348 	 */
349 	cp = (char *)eh + len;	/* points at trailing CRC */
350 	if (len & 1) {
351 		csum += 0xffff ^ (u16)((cp[1] << 8) | cp[0]);
352 		csum += 0xffff ^ (u16)((cp[3] << 8) | cp[2]);
353 	} else {
354 		csum += 0xffff ^ (u16)((cp[0] << 8) | cp[1]);
355 		csum += 0xffff ^ (u16)((cp[2] << 8) | cp[3]);
356 	}
357 
358 	csum = (csum & 0xffff) + (csum >> 16);
359 	csum = (csum & 0xffff) + (csum >> 16);
360 
361 	if (csum == 0xffff)
362 		skb->ip_summed = CHECKSUM_UNNECESSARY;
363 }
364 
365 static inline void ioc3_rx(struct net_device *dev)
366 {
367 	struct ioc3_private *ip = netdev_priv(dev);
368 	struct sk_buff *skb, *new_skb;
369 	int rx_entry, n_entry, len;
370 	struct ioc3_erxbuf *rxb;
371 	unsigned long *rxr;
372 	dma_addr_t d;
373 	u32 w0, err;
374 
375 	rxr = ip->rxr;		/* Ring base */
376 	rx_entry = ip->rx_ci;				/* RX consume index */
377 	n_entry = ip->rx_pi;
378 
379 	skb = ip->rx_skbs[rx_entry];
380 	rxb = (struct ioc3_erxbuf *)(skb->data - RX_OFFSET);
381 	w0 = be32_to_cpu(rxb->w0);
382 
383 	while (w0 & ERXBUF_V) {
384 		err = be32_to_cpu(rxb->err);		/* It's valid ...  */
385 		if (err & ERXBUF_GOODPKT) {
386 			len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
387 			skb_put(skb, len);
388 			skb->protocol = eth_type_trans(skb, dev);
389 
390 			if (ioc3_alloc_skb(ip, &new_skb, &rxb, &d)) {
391 				/* Ouch, drop packet and just recycle packet
392 				 * to keep the ring filled.
393 				 */
394 				dev->stats.rx_dropped++;
395 				new_skb = skb;
396 				d = rxr[rx_entry];
397 				goto next;
398 			}
399 
400 			if (likely(dev->features & NETIF_F_RXCSUM))
401 				ioc3_tcpudp_checksum(skb,
402 						     w0 & ERXBUF_IPCKSUM_MASK,
403 						     len);
404 
405 			dma_unmap_single(ip->dma_dev, rxr[rx_entry],
406 					 RX_BUF_SIZE, DMA_FROM_DEVICE);
407 
408 			netif_rx(skb);
409 
410 			ip->rx_skbs[rx_entry] = NULL;	/* Poison  */
411 
412 			dev->stats.rx_packets++;		/* Statistics */
413 			dev->stats.rx_bytes += len;
414 		} else {
415 			/* The frame is invalid and the skb never
416 			 * reached the network layer so we can just
417 			 * recycle it.
418 			 */
419 			new_skb = skb;
420 			d = rxr[rx_entry];
421 			dev->stats.rx_errors++;
422 		}
423 		if (err & ERXBUF_CRCERR)	/* Statistics */
424 			dev->stats.rx_crc_errors++;
425 		if (err & ERXBUF_FRAMERR)
426 			dev->stats.rx_frame_errors++;
427 
428 next:
429 		ip->rx_skbs[n_entry] = new_skb;
430 		rxr[n_entry] = cpu_to_be64(ioc3_map(d, PCI64_ATTR_BAR));
431 		rxb->w0 = 0;				/* Clear valid flag */
432 		n_entry = (n_entry + 1) & RX_RING_MASK;	/* Update erpir */
433 
434 		/* Now go on to the next ring entry.  */
435 		rx_entry = (rx_entry + 1) & RX_RING_MASK;
436 		skb = ip->rx_skbs[rx_entry];
437 		rxb = (struct ioc3_erxbuf *)(skb->data - RX_OFFSET);
438 		w0 = be32_to_cpu(rxb->w0);
439 	}
440 	writel((n_entry << 3) | ERPIR_ARM, &ip->regs->erpir);
441 	ip->rx_pi = n_entry;
442 	ip->rx_ci = rx_entry;
443 }
444 
445 static inline void ioc3_tx(struct net_device *dev)
446 {
447 	struct ioc3_private *ip = netdev_priv(dev);
448 	struct ioc3_ethregs *regs = ip->regs;
449 	unsigned long packets, bytes;
450 	int tx_entry, o_entry;
451 	struct sk_buff *skb;
452 	u32 etcir;
453 
454 	spin_lock(&ip->ioc3_lock);
455 	etcir = readl(&regs->etcir);
456 
457 	tx_entry = (etcir >> 7) & TX_RING_MASK;
458 	o_entry = ip->tx_ci;
459 	packets = 0;
460 	bytes = 0;
461 
462 	while (o_entry != tx_entry) {
463 		packets++;
464 		skb = ip->tx_skbs[o_entry];
465 		bytes += skb->len;
466 		dev_consume_skb_irq(skb);
467 		ip->tx_skbs[o_entry] = NULL;
468 
469 		o_entry = (o_entry + 1) & TX_RING_MASK;	/* Next */
470 
471 		etcir = readl(&regs->etcir);		/* More pkts sent?  */
472 		tx_entry = (etcir >> 7) & TX_RING_MASK;
473 	}
474 
475 	dev->stats.tx_packets += packets;
476 	dev->stats.tx_bytes += bytes;
477 	ip->txqlen -= packets;
478 
479 	if (netif_queue_stopped(dev) && ip->txqlen < TX_RING_ENTRIES)
480 		netif_wake_queue(dev);
481 
482 	ip->tx_ci = o_entry;
483 	spin_unlock(&ip->ioc3_lock);
484 }
485 
486 /* Deal with fatal IOC3 errors.  This condition might be caused by a hard or
487  * software problems, so we should try to recover
488  * more gracefully if this ever happens.  In theory we might be flooded
489  * with such error interrupts if something really goes wrong, so we might
490  * also consider to take the interface down.
491  */
492 static void ioc3_error(struct net_device *dev, u32 eisr)
493 {
494 	struct ioc3_private *ip = netdev_priv(dev);
495 
496 	spin_lock(&ip->ioc3_lock);
497 
498 	if (eisr & EISR_RXOFLO)
499 		net_err_ratelimited("%s: RX overflow.\n", dev->name);
500 	if (eisr & EISR_RXBUFOFLO)
501 		net_err_ratelimited("%s: RX buffer overflow.\n", dev->name);
502 	if (eisr & EISR_RXMEMERR)
503 		net_err_ratelimited("%s: RX PCI error.\n", dev->name);
504 	if (eisr & EISR_RXPARERR)
505 		net_err_ratelimited("%s: RX SSRAM parity error.\n", dev->name);
506 	if (eisr & EISR_TXBUFUFLO)
507 		net_err_ratelimited("%s: TX buffer underflow.\n", dev->name);
508 	if (eisr & EISR_TXMEMERR)
509 		net_err_ratelimited("%s: TX PCI error.\n", dev->name);
510 
511 	ioc3_stop(ip);
512 	ioc3_free_rx_bufs(ip);
513 	ioc3_clean_tx_ring(ip);
514 
515 	ioc3_init(dev);
516 	if (ioc3_alloc_rx_bufs(dev)) {
517 		netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
518 		spin_unlock(&ip->ioc3_lock);
519 		return;
520 	}
521 	ioc3_start(ip);
522 	ioc3_mii_init(ip);
523 
524 	netif_wake_queue(dev);
525 
526 	spin_unlock(&ip->ioc3_lock);
527 }
528 
529 /* The interrupt handler does all of the Rx thread work and cleans up
530  * after the Tx thread.
531  */
532 static irqreturn_t ioc3_interrupt(int irq, void *dev_id)
533 {
534 	struct ioc3_private *ip = netdev_priv(dev_id);
535 	struct ioc3_ethregs *regs = ip->regs;
536 	u32 eisr;
537 
538 	eisr = readl(&regs->eisr);
539 	writel(eisr, &regs->eisr);
540 	readl(&regs->eisr);				/* Flush */
541 
542 	if (eisr & (EISR_RXOFLO | EISR_RXBUFOFLO | EISR_RXMEMERR |
543 		    EISR_RXPARERR | EISR_TXBUFUFLO | EISR_TXMEMERR))
544 		ioc3_error(dev_id, eisr);
545 	if (eisr & EISR_RXTIMERINT)
546 		ioc3_rx(dev_id);
547 	if (eisr & EISR_TXEXPLICIT)
548 		ioc3_tx(dev_id);
549 
550 	return IRQ_HANDLED;
551 }
552 
553 static inline void ioc3_setup_duplex(struct ioc3_private *ip)
554 {
555 	struct ioc3_ethregs *regs = ip->regs;
556 
557 	spin_lock_irq(&ip->ioc3_lock);
558 
559 	if (ip->mii.full_duplex) {
560 		writel(ETCSR_FD, &regs->etcsr);
561 		ip->emcr |= EMCR_DUPLEX;
562 	} else {
563 		writel(ETCSR_HD, &regs->etcsr);
564 		ip->emcr &= ~EMCR_DUPLEX;
565 	}
566 	writel(ip->emcr, &regs->emcr);
567 
568 	spin_unlock_irq(&ip->ioc3_lock);
569 }
570 
571 static void ioc3_timer(struct timer_list *t)
572 {
573 	struct ioc3_private *ip = from_timer(ip, t, ioc3_timer);
574 
575 	/* Print the link status if it has changed */
576 	mii_check_media(&ip->mii, 1, 0);
577 	ioc3_setup_duplex(ip);
578 
579 	ip->ioc3_timer.expires = jiffies + ((12 * HZ) / 10); /* 1.2s */
580 	add_timer(&ip->ioc3_timer);
581 }
582 
583 /* Try to find a PHY.  There is no apparent relation between the MII addresses
584  * in the SGI documentation and what we find in reality, so we simply probe
585  * for the PHY.  It seems IOC3 PHYs usually live on address 31.  One of my
586  * onboard IOC3s has the special oddity that probing doesn't seem to find it
587  * yet the interface seems to work fine, so if probing fails we for now will
588  * simply default to PHY 31 instead of bailing out.
589  */
590 static int ioc3_mii_init(struct ioc3_private *ip)
591 {
592 	int ioc3_phy_workaround = 1;
593 	int i, found = 0, res = 0;
594 	u16 word;
595 
596 	for (i = 0; i < 32; i++) {
597 		word = ioc3_mdio_read(ip->mii.dev, i, MII_PHYSID1);
598 
599 		if (word != 0xffff && word != 0x0000) {
600 			found = 1;
601 			break;			/* Found a PHY		*/
602 		}
603 	}
604 
605 	if (!found) {
606 		if (ioc3_phy_workaround) {
607 			i = 31;
608 		} else {
609 			ip->mii.phy_id = -1;
610 			res = -ENODEV;
611 			goto out;
612 		}
613 	}
614 
615 	ip->mii.phy_id = i;
616 
617 out:
618 	return res;
619 }
620 
621 static void ioc3_mii_start(struct ioc3_private *ip)
622 {
623 	ip->ioc3_timer.expires = jiffies + (12 * HZ) / 10;  /* 1.2 sec. */
624 	add_timer(&ip->ioc3_timer);
625 }
626 
627 static inline void ioc3_tx_unmap(struct ioc3_private *ip, int entry)
628 {
629 	struct ioc3_etxd *desc;
630 	u32 cmd, bufcnt, len;
631 
632 	desc = &ip->txr[entry];
633 	cmd = be32_to_cpu(desc->cmd);
634 	bufcnt = be32_to_cpu(desc->bufcnt);
635 	if (cmd & ETXD_B1V) {
636 		len = (bufcnt & ETXD_B1CNT_MASK) >> ETXD_B1CNT_SHIFT;
637 		dma_unmap_single(ip->dma_dev, be64_to_cpu(desc->p1),
638 				 len, DMA_TO_DEVICE);
639 	}
640 	if (cmd & ETXD_B2V) {
641 		len = (bufcnt & ETXD_B2CNT_MASK) >> ETXD_B2CNT_SHIFT;
642 		dma_unmap_single(ip->dma_dev, be64_to_cpu(desc->p2),
643 				 len, DMA_TO_DEVICE);
644 	}
645 }
646 
647 static inline void ioc3_clean_tx_ring(struct ioc3_private *ip)
648 {
649 	struct sk_buff *skb;
650 	int i;
651 
652 	for (i = 0; i < TX_RING_ENTRIES; i++) {
653 		skb = ip->tx_skbs[i];
654 		if (skb) {
655 			ioc3_tx_unmap(ip, i);
656 			ip->tx_skbs[i] = NULL;
657 			dev_kfree_skb_any(skb);
658 		}
659 		ip->txr[i].cmd = 0;
660 	}
661 	ip->tx_pi = 0;
662 	ip->tx_ci = 0;
663 }
664 
665 static void ioc3_free_rx_bufs(struct ioc3_private *ip)
666 {
667 	int rx_entry, n_entry;
668 	struct sk_buff *skb;
669 
670 	n_entry = ip->rx_ci;
671 	rx_entry = ip->rx_pi;
672 
673 	while (n_entry != rx_entry) {
674 		skb = ip->rx_skbs[n_entry];
675 		if (skb) {
676 			dma_unmap_single(ip->dma_dev,
677 					 be64_to_cpu(ip->rxr[n_entry]),
678 					 RX_BUF_SIZE, DMA_FROM_DEVICE);
679 			dev_kfree_skb_any(skb);
680 		}
681 		n_entry = (n_entry + 1) & RX_RING_MASK;
682 	}
683 }
684 
685 static int ioc3_alloc_rx_bufs(struct net_device *dev)
686 {
687 	struct ioc3_private *ip = netdev_priv(dev);
688 	struct ioc3_erxbuf *rxb;
689 	dma_addr_t d;
690 	int i;
691 
692 	/* Now the rx buffers.  The RX ring may be larger but
693 	 * we only allocate 16 buffers for now.  Need to tune
694 	 * this for performance and memory later.
695 	 */
696 	for (i = 0; i < RX_BUFFS; i++) {
697 		if (ioc3_alloc_skb(ip, &ip->rx_skbs[i], &rxb, &d))
698 			return -ENOMEM;
699 
700 		rxb->w0 = 0;	/* Clear valid flag */
701 		ip->rxr[i] = cpu_to_be64(ioc3_map(d, PCI64_ATTR_BAR));
702 	}
703 	ip->rx_ci = 0;
704 	ip->rx_pi = RX_BUFFS;
705 
706 	return 0;
707 }
708 
709 static inline void ioc3_ssram_disc(struct ioc3_private *ip)
710 {
711 	struct ioc3_ethregs *regs = ip->regs;
712 	u32 *ssram0 = &ip->ssram[0x0000];
713 	u32 *ssram1 = &ip->ssram[0x4000];
714 	u32 pattern = 0x5555;
715 
716 	/* Assume the larger size SSRAM and enable parity checking */
717 	writel(readl(&regs->emcr) | (EMCR_BUFSIZ | EMCR_RAMPAR), &regs->emcr);
718 	readl(&regs->emcr); /* Flush */
719 
720 	writel(pattern, ssram0);
721 	writel(~pattern & IOC3_SSRAM_DM, ssram1);
722 
723 	if ((readl(ssram0) & IOC3_SSRAM_DM) != pattern ||
724 	    (readl(ssram1) & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
725 		/* set ssram size to 64 KB */
726 		ip->emcr |= EMCR_RAMPAR;
727 		writel(readl(&regs->emcr) & ~EMCR_BUFSIZ, &regs->emcr);
728 	} else {
729 		ip->emcr |= EMCR_BUFSIZ | EMCR_RAMPAR;
730 	}
731 }
732 
733 static void ioc3_init(struct net_device *dev)
734 {
735 	struct ioc3_private *ip = netdev_priv(dev);
736 	struct ioc3_ethregs *regs = ip->regs;
737 
738 	del_timer_sync(&ip->ioc3_timer);	/* Kill if running	*/
739 
740 	writel(EMCR_RST, &regs->emcr);		/* Reset		*/
741 	readl(&regs->emcr);			/* Flush WB		*/
742 	udelay(4);				/* Give it time ...	*/
743 	writel(0, &regs->emcr);
744 	readl(&regs->emcr);
745 
746 	/* Misc registers  */
747 	writel(ERBAR_VAL, &regs->erbar);
748 	readl(&regs->etcdc);			/* Clear on read */
749 	writel(15, &regs->ercsr);		/* RX low watermark  */
750 	writel(0, &regs->ertr);			/* Interrupt immediately */
751 	__ioc3_set_mac_address(dev);
752 	writel(ip->ehar_h, &regs->ehar_h);
753 	writel(ip->ehar_l, &regs->ehar_l);
754 	writel(42, &regs->ersr);		/* XXX should be random */
755 }
756 
757 static void ioc3_start(struct ioc3_private *ip)
758 {
759 	struct ioc3_ethregs *regs = ip->regs;
760 	unsigned long ring;
761 
762 	/* Now the rx ring base, consume & produce registers.  */
763 	ring = ioc3_map(ip->rxr_dma, PCI64_ATTR_PREC);
764 	writel(ring >> 32, &regs->erbr_h);
765 	writel(ring & 0xffffffff, &regs->erbr_l);
766 	writel(ip->rx_ci << 3, &regs->ercir);
767 	writel((ip->rx_pi << 3) | ERPIR_ARM, &regs->erpir);
768 
769 	ring = ioc3_map(ip->txr_dma, PCI64_ATTR_PREC);
770 
771 	ip->txqlen = 0;					/* nothing queued  */
772 
773 	/* Now the tx ring base, consume & produce registers.  */
774 	writel(ring >> 32, &regs->etbr_h);
775 	writel(ring & 0xffffffff, &regs->etbr_l);
776 	writel(ip->tx_pi << 7, &regs->etpir);
777 	writel(ip->tx_ci << 7, &regs->etcir);
778 	readl(&regs->etcir);				/* Flush */
779 
780 	ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
781 		    EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN | EMCR_PADEN;
782 	writel(ip->emcr, &regs->emcr);
783 	writel(EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
784 	       EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
785 	       EISR_TXEXPLICIT | EISR_TXMEMERR, &regs->eier);
786 	readl(&regs->eier);
787 }
788 
789 static inline void ioc3_stop(struct ioc3_private *ip)
790 {
791 	struct ioc3_ethregs *regs = ip->regs;
792 
793 	writel(0, &regs->emcr);			/* Shutup */
794 	writel(0, &regs->eier);			/* Disable interrupts */
795 	readl(&regs->eier);			/* Flush */
796 }
797 
798 static int ioc3_open(struct net_device *dev)
799 {
800 	struct ioc3_private *ip = netdev_priv(dev);
801 
802 	ip->ehar_h = 0;
803 	ip->ehar_l = 0;
804 
805 	ioc3_init(dev);
806 	if (ioc3_alloc_rx_bufs(dev)) {
807 		netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
808 		return -ENOMEM;
809 	}
810 	ioc3_start(ip);
811 	ioc3_mii_start(ip);
812 
813 	netif_start_queue(dev);
814 	return 0;
815 }
816 
817 static int ioc3_close(struct net_device *dev)
818 {
819 	struct ioc3_private *ip = netdev_priv(dev);
820 
821 	del_timer_sync(&ip->ioc3_timer);
822 
823 	netif_stop_queue(dev);
824 
825 	ioc3_stop(ip);
826 
827 	ioc3_free_rx_bufs(ip);
828 	ioc3_clean_tx_ring(ip);
829 
830 	return 0;
831 }
832 
833 static const struct net_device_ops ioc3_netdev_ops = {
834 	.ndo_open		= ioc3_open,
835 	.ndo_stop		= ioc3_close,
836 	.ndo_start_xmit		= ioc3_start_xmit,
837 	.ndo_tx_timeout		= ioc3_timeout,
838 	.ndo_get_stats		= ioc3_get_stats,
839 	.ndo_set_rx_mode	= ioc3_set_multicast_list,
840 	.ndo_do_ioctl		= ioc3_ioctl,
841 	.ndo_validate_addr	= eth_validate_addr,
842 	.ndo_set_mac_address	= ioc3_set_mac_address,
843 };
844 
845 static int ioc3eth_probe(struct platform_device *pdev)
846 {
847 	u32 sw_physid1, sw_physid2, vendor, model, rev;
848 	struct ioc3_private *ip;
849 	struct net_device *dev;
850 	struct resource *regs;
851 	u8 mac_addr[6];
852 	int err;
853 
854 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
855 	/* get mac addr from one wire prom */
856 	if (ioc3eth_get_mac_addr(regs, mac_addr))
857 		return -EPROBE_DEFER; /* not available yet */
858 
859 	dev = alloc_etherdev(sizeof(struct ioc3_private));
860 	if (!dev)
861 		return -ENOMEM;
862 
863 	SET_NETDEV_DEV(dev, &pdev->dev);
864 
865 	ip = netdev_priv(dev);
866 	ip->dma_dev = pdev->dev.parent;
867 	ip->regs = devm_platform_ioremap_resource(pdev, 0);
868 	if (!ip->regs) {
869 		err = -ENOMEM;
870 		goto out_free;
871 	}
872 
873 	ip->ssram = devm_platform_ioremap_resource(pdev, 1);
874 	if (!ip->ssram) {
875 		err = -ENOMEM;
876 		goto out_free;
877 	}
878 
879 	dev->irq = platform_get_irq(pdev, 0);
880 	if (dev->irq < 0) {
881 		err = dev->irq;
882 		goto out_free;
883 	}
884 
885 	if (devm_request_irq(&pdev->dev, dev->irq, ioc3_interrupt,
886 			     IRQF_SHARED, "ioc3-eth", dev)) {
887 		dev_err(&pdev->dev, "Can't get irq %d\n", dev->irq);
888 		err = -ENODEV;
889 		goto out_free;
890 	}
891 
892 	spin_lock_init(&ip->ioc3_lock);
893 	timer_setup(&ip->ioc3_timer, ioc3_timer, 0);
894 
895 	ioc3_stop(ip);
896 
897 	/* Allocate rx ring.  4kb = 512 entries, must be 4kb aligned */
898 	ip->rxr = dma_alloc_coherent(ip->dma_dev, RX_RING_SIZE, &ip->rxr_dma,
899 				     GFP_KERNEL);
900 	if (!ip->rxr) {
901 		pr_err("ioc3-eth: rx ring allocation failed\n");
902 		err = -ENOMEM;
903 		goto out_stop;
904 	}
905 
906 	/* Allocate tx rings.  16kb = 128 bufs, must be 16kb aligned  */
907 	ip->tx_ring = dma_alloc_coherent(ip->dma_dev, TX_RING_SIZE + SZ_16K - 1,
908 					 &ip->txr_dma, GFP_KERNEL);
909 	if (!ip->tx_ring) {
910 		pr_err("ioc3-eth: tx ring allocation failed\n");
911 		err = -ENOMEM;
912 		goto out_stop;
913 	}
914 	/* Align TX ring */
915 	ip->txr = PTR_ALIGN(ip->tx_ring, SZ_16K);
916 	ip->txr_dma = ALIGN(ip->txr_dma, SZ_16K);
917 
918 	ioc3_init(dev);
919 
920 	ip->mii.phy_id_mask = 0x1f;
921 	ip->mii.reg_num_mask = 0x1f;
922 	ip->mii.dev = dev;
923 	ip->mii.mdio_read = ioc3_mdio_read;
924 	ip->mii.mdio_write = ioc3_mdio_write;
925 
926 	ioc3_mii_init(ip);
927 
928 	if (ip->mii.phy_id == -1) {
929 		netdev_err(dev, "Didn't find a PHY, goodbye.\n");
930 		err = -ENODEV;
931 		goto out_stop;
932 	}
933 
934 	ioc3_mii_start(ip);
935 	ioc3_ssram_disc(ip);
936 	memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
937 
938 	/* The IOC3-specific entries in the device structure. */
939 	dev->watchdog_timeo	= 5 * HZ;
940 	dev->netdev_ops		= &ioc3_netdev_ops;
941 	dev->ethtool_ops	= &ioc3_ethtool_ops;
942 	dev->hw_features	= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
943 	dev->features		= NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
944 
945 	sw_physid1 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID1);
946 	sw_physid2 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID2);
947 
948 	err = register_netdev(dev);
949 	if (err)
950 		goto out_stop;
951 
952 	mii_check_media(&ip->mii, 1, 1);
953 	ioc3_setup_duplex(ip);
954 
955 	vendor = (sw_physid1 << 12) | (sw_physid2 >> 4);
956 	model  = (sw_physid2 >> 4) & 0x3f;
957 	rev    = sw_physid2 & 0xf;
958 	netdev_info(dev, "Using PHY %d, vendor 0x%x, model %d, rev %d.\n",
959 		    ip->mii.phy_id, vendor, model, rev);
960 	netdev_info(dev, "IOC3 SSRAM has %d kbyte.\n",
961 		    ip->emcr & EMCR_BUFSIZ ? 128 : 64);
962 
963 	return 0;
964 
965 out_stop:
966 	del_timer_sync(&ip->ioc3_timer);
967 	if (ip->rxr)
968 		dma_free_coherent(ip->dma_dev, RX_RING_SIZE, ip->rxr,
969 				  ip->rxr_dma);
970 	if (ip->tx_ring)
971 		dma_free_coherent(ip->dma_dev, TX_RING_SIZE, ip->tx_ring,
972 				  ip->txr_dma);
973 out_free:
974 	free_netdev(dev);
975 	return err;
976 }
977 
978 static int ioc3eth_remove(struct platform_device *pdev)
979 {
980 	struct net_device *dev = platform_get_drvdata(pdev);
981 	struct ioc3_private *ip = netdev_priv(dev);
982 
983 	dma_free_coherent(ip->dma_dev, RX_RING_SIZE, ip->rxr, ip->rxr_dma);
984 	dma_free_coherent(ip->dma_dev, TX_RING_SIZE, ip->tx_ring, ip->txr_dma);
985 
986 	unregister_netdev(dev);
987 	del_timer_sync(&ip->ioc3_timer);
988 	free_netdev(dev);
989 
990 	return 0;
991 }
992 
993 
994 static netdev_tx_t ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
995 {
996 	struct ioc3_private *ip = netdev_priv(dev);
997 	struct ioc3_etxd *desc;
998 	unsigned long data;
999 	unsigned int len;
1000 	int produce;
1001 	u32 w0 = 0;
1002 
1003 	/* IOC3 has a fairly simple minded checksumming hardware which simply
1004 	 * adds up the 1's complement checksum for the entire packet and
1005 	 * inserts it at an offset which can be specified in the descriptor
1006 	 * into the transmit packet.  This means we have to compensate for the
1007 	 * MAC header which should not be summed and the TCP/UDP pseudo headers
1008 	 * manually.
1009 	 */
1010 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1011 		const struct iphdr *ih = ip_hdr(skb);
1012 		const int proto = ntohs(ih->protocol);
1013 		unsigned int csoff;
1014 		u32 csum, ehsum;
1015 		u16 *eh;
1016 
1017 		/* The MAC header.  skb->mac seem the logic approach
1018 		 * to find the MAC header - except it's a NULL pointer ...
1019 		 */
1020 		eh = (u16 *)skb->data;
1021 
1022 		/* Sum up dest addr, src addr and protocol  */
1023 		ehsum = eh[0] + eh[1] + eh[2] + eh[3] + eh[4] + eh[5] + eh[6];
1024 
1025 		/* Skip IP header; it's sum is always zero and was
1026 		 * already filled in by ip_output.c
1027 		 */
1028 		csum = csum_tcpudp_nofold(ih->saddr, ih->daddr,
1029 					  ih->tot_len - (ih->ihl << 2),
1030 					  proto, csum_fold(ehsum));
1031 
1032 		csum = (csum & 0xffff) + (csum >> 16);	/* Fold again */
1033 		csum = (csum & 0xffff) + (csum >> 16);
1034 
1035 		csoff = ETH_HLEN + (ih->ihl << 2);
1036 		if (proto == IPPROTO_UDP) {
1037 			csoff += offsetof(struct udphdr, check);
1038 			udp_hdr(skb)->check = csum;
1039 		}
1040 		if (proto == IPPROTO_TCP) {
1041 			csoff += offsetof(struct tcphdr, check);
1042 			tcp_hdr(skb)->check = csum;
1043 		}
1044 
1045 		w0 = ETXD_DOCHECKSUM | (csoff << ETXD_CHKOFF_SHIFT);
1046 	}
1047 
1048 	spin_lock_irq(&ip->ioc3_lock);
1049 
1050 	data = (unsigned long)skb->data;
1051 	len = skb->len;
1052 
1053 	produce = ip->tx_pi;
1054 	desc = &ip->txr[produce];
1055 
1056 	if (len <= 104) {
1057 		/* Short packet, let's copy it directly into the ring.  */
1058 		skb_copy_from_linear_data(skb, desc->data, skb->len);
1059 		if (len < ETH_ZLEN) {
1060 			/* Very short packet, pad with zeros at the end. */
1061 			memset(desc->data + len, 0, ETH_ZLEN - len);
1062 			len = ETH_ZLEN;
1063 		}
1064 		desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V | w0);
1065 		desc->bufcnt = cpu_to_be32(len);
1066 	} else if ((data ^ (data + len - 1)) & 0x4000) {
1067 		unsigned long b2 = (data | 0x3fffUL) + 1UL;
1068 		unsigned long s1 = b2 - data;
1069 		unsigned long s2 = data + len - b2;
1070 		dma_addr_t d1, d2;
1071 
1072 		desc->cmd    = cpu_to_be32(len | ETXD_INTWHENDONE |
1073 					   ETXD_B1V | ETXD_B2V | w0);
1074 		desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT) |
1075 					   (s2 << ETXD_B2CNT_SHIFT));
1076 		d1 = dma_map_single(ip->dma_dev, skb->data, s1, DMA_TO_DEVICE);
1077 		if (dma_mapping_error(ip->dma_dev, d1))
1078 			goto drop_packet;
1079 		d2 = dma_map_single(ip->dma_dev, (void *)b2, s1, DMA_TO_DEVICE);
1080 		if (dma_mapping_error(ip->dma_dev, d2)) {
1081 			dma_unmap_single(ip->dma_dev, d1, len, DMA_TO_DEVICE);
1082 			goto drop_packet;
1083 		}
1084 		desc->p1     = cpu_to_be64(ioc3_map(d1, PCI64_ATTR_PREF));
1085 		desc->p2     = cpu_to_be64(ioc3_map(d2, PCI64_ATTR_PREF));
1086 	} else {
1087 		dma_addr_t d;
1088 
1089 		/* Normal sized packet that doesn't cross a page boundary. */
1090 		desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V | w0);
1091 		desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
1092 		d = dma_map_single(ip->dma_dev, skb->data, len, DMA_TO_DEVICE);
1093 		if (dma_mapping_error(ip->dma_dev, d))
1094 			goto drop_packet;
1095 		desc->p1     = cpu_to_be64(ioc3_map(d, PCI64_ATTR_PREF));
1096 	}
1097 
1098 	mb(); /* make sure all descriptor changes are visible */
1099 
1100 	ip->tx_skbs[produce] = skb;			/* Remember skb */
1101 	produce = (produce + 1) & TX_RING_MASK;
1102 	ip->tx_pi = produce;
1103 	writel(produce << 7, &ip->regs->etpir);		/* Fire ... */
1104 
1105 	ip->txqlen++;
1106 
1107 	if (ip->txqlen >= (TX_RING_ENTRIES - 1))
1108 		netif_stop_queue(dev);
1109 
1110 	spin_unlock_irq(&ip->ioc3_lock);
1111 
1112 	return NETDEV_TX_OK;
1113 
1114 drop_packet:
1115 	dev_kfree_skb_any(skb);
1116 	dev->stats.tx_dropped++;
1117 
1118 	spin_unlock_irq(&ip->ioc3_lock);
1119 
1120 	return NETDEV_TX_OK;
1121 }
1122 
1123 static void ioc3_timeout(struct net_device *dev, unsigned int txqueue)
1124 {
1125 	struct ioc3_private *ip = netdev_priv(dev);
1126 
1127 	netdev_err(dev, "transmit timed out, resetting\n");
1128 
1129 	spin_lock_irq(&ip->ioc3_lock);
1130 
1131 	ioc3_stop(ip);
1132 	ioc3_free_rx_bufs(ip);
1133 	ioc3_clean_tx_ring(ip);
1134 
1135 	ioc3_init(dev);
1136 	if (ioc3_alloc_rx_bufs(dev)) {
1137 		netdev_err(dev, "%s: rx buffer allocation failed\n", __func__);
1138 		spin_unlock_irq(&ip->ioc3_lock);
1139 		return;
1140 	}
1141 	ioc3_start(ip);
1142 	ioc3_mii_init(ip);
1143 	ioc3_mii_start(ip);
1144 
1145 	spin_unlock_irq(&ip->ioc3_lock);
1146 
1147 	netif_wake_queue(dev);
1148 }
1149 
1150 /* Given a multicast ethernet address, this routine calculates the
1151  * address's bit index in the logical address filter mask
1152  */
1153 static inline unsigned int ioc3_hash(const unsigned char *addr)
1154 {
1155 	unsigned int temp = 0;
1156 	int bits;
1157 	u32 crc;
1158 
1159 	crc = ether_crc_le(ETH_ALEN, addr);
1160 
1161 	crc &= 0x3f;    /* bit reverse lowest 6 bits for hash index */
1162 	for (bits = 6; --bits >= 0; ) {
1163 		temp <<= 1;
1164 		temp |= (crc & 0x1);
1165 		crc >>= 1;
1166 	}
1167 
1168 	return temp;
1169 }
1170 
1171 static void ioc3_get_drvinfo(struct net_device *dev,
1172 			     struct ethtool_drvinfo *info)
1173 {
1174 	strlcpy(info->driver, IOC3_NAME, sizeof(info->driver));
1175 	strlcpy(info->version, IOC3_VERSION, sizeof(info->version));
1176 	strlcpy(info->bus_info, pci_name(to_pci_dev(dev->dev.parent)),
1177 		sizeof(info->bus_info));
1178 }
1179 
1180 static int ioc3_get_link_ksettings(struct net_device *dev,
1181 				   struct ethtool_link_ksettings *cmd)
1182 {
1183 	struct ioc3_private *ip = netdev_priv(dev);
1184 
1185 	spin_lock_irq(&ip->ioc3_lock);
1186 	mii_ethtool_get_link_ksettings(&ip->mii, cmd);
1187 	spin_unlock_irq(&ip->ioc3_lock);
1188 
1189 	return 0;
1190 }
1191 
1192 static int ioc3_set_link_ksettings(struct net_device *dev,
1193 				   const struct ethtool_link_ksettings *cmd)
1194 {
1195 	struct ioc3_private *ip = netdev_priv(dev);
1196 	int rc;
1197 
1198 	spin_lock_irq(&ip->ioc3_lock);
1199 	rc = mii_ethtool_set_link_ksettings(&ip->mii, cmd);
1200 	spin_unlock_irq(&ip->ioc3_lock);
1201 
1202 	return rc;
1203 }
1204 
1205 static int ioc3_nway_reset(struct net_device *dev)
1206 {
1207 	struct ioc3_private *ip = netdev_priv(dev);
1208 	int rc;
1209 
1210 	spin_lock_irq(&ip->ioc3_lock);
1211 	rc = mii_nway_restart(&ip->mii);
1212 	spin_unlock_irq(&ip->ioc3_lock);
1213 
1214 	return rc;
1215 }
1216 
1217 static u32 ioc3_get_link(struct net_device *dev)
1218 {
1219 	struct ioc3_private *ip = netdev_priv(dev);
1220 	int rc;
1221 
1222 	spin_lock_irq(&ip->ioc3_lock);
1223 	rc = mii_link_ok(&ip->mii);
1224 	spin_unlock_irq(&ip->ioc3_lock);
1225 
1226 	return rc;
1227 }
1228 
1229 static const struct ethtool_ops ioc3_ethtool_ops = {
1230 	.get_drvinfo		= ioc3_get_drvinfo,
1231 	.nway_reset		= ioc3_nway_reset,
1232 	.get_link		= ioc3_get_link,
1233 	.get_link_ksettings	= ioc3_get_link_ksettings,
1234 	.set_link_ksettings	= ioc3_set_link_ksettings,
1235 };
1236 
1237 static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1238 {
1239 	struct ioc3_private *ip = netdev_priv(dev);
1240 	int rc;
1241 
1242 	spin_lock_irq(&ip->ioc3_lock);
1243 	rc = generic_mii_ioctl(&ip->mii, if_mii(rq), cmd, NULL);
1244 	spin_unlock_irq(&ip->ioc3_lock);
1245 
1246 	return rc;
1247 }
1248 
1249 static void ioc3_set_multicast_list(struct net_device *dev)
1250 {
1251 	struct ioc3_private *ip = netdev_priv(dev);
1252 	struct ioc3_ethregs *regs = ip->regs;
1253 	struct netdev_hw_addr *ha;
1254 	u64 ehar = 0;
1255 
1256 	spin_lock_irq(&ip->ioc3_lock);
1257 
1258 	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous.  */
1259 		ip->emcr |= EMCR_PROMISC;
1260 		writel(ip->emcr, &regs->emcr);
1261 		readl(&regs->emcr);
1262 	} else {
1263 		ip->emcr &= ~EMCR_PROMISC;
1264 		writel(ip->emcr, &regs->emcr);		/* Clear promiscuous. */
1265 		readl(&regs->emcr);
1266 
1267 		if ((dev->flags & IFF_ALLMULTI) ||
1268 		    (netdev_mc_count(dev) > 64)) {
1269 			/* Too many for hashing to make sense or we want all
1270 			 * multicast packets anyway,  so skip computing all the
1271 			 * hashes and just accept all packets.
1272 			 */
1273 			ip->ehar_h = 0xffffffff;
1274 			ip->ehar_l = 0xffffffff;
1275 		} else {
1276 			netdev_for_each_mc_addr(ha, dev) {
1277 				ehar |= (1UL << ioc3_hash(ha->addr));
1278 			}
1279 			ip->ehar_h = ehar >> 32;
1280 			ip->ehar_l = ehar & 0xffffffff;
1281 		}
1282 		writel(ip->ehar_h, &regs->ehar_h);
1283 		writel(ip->ehar_l, &regs->ehar_l);
1284 	}
1285 
1286 	spin_unlock_irq(&ip->ioc3_lock);
1287 }
1288 
1289 static struct platform_driver ioc3eth_driver = {
1290 	.probe  = ioc3eth_probe,
1291 	.remove = ioc3eth_remove,
1292 	.driver = {
1293 		.name = "ioc3-eth",
1294 	}
1295 };
1296 
1297 module_platform_driver(ioc3eth_driver);
1298 
1299 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1300 MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
1301 MODULE_LICENSE("GPL");
1302