xref: /linux/drivers/net/ethernet/sun/cassini.c (revision 0b87d2ab030fd969ea626b2da95594949a2c0508)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* cassini.c: Sun Microsystems Cassini(+) ethernet driver.
3  *
4  * Copyright (C) 2004 Sun Microsystems Inc.
5  * Copyright (C) 2003 Adrian Sun (asun@darksunrising.com)
6  *
7  * This driver uses the sungem driver (c) David Miller
8  * (davem@redhat.com) as its basis.
9  *
10  * The cassini chip has a number of features that distinguish it from
11  * the gem chip:
12  *  4 transmit descriptor rings that are used for either QoS (VLAN) or
13  *      load balancing (non-VLAN mode)
14  *  batching of multiple packets
15  *  multiple CPU dispatching
16  *  page-based RX descriptor engine with separate completion rings
17  *  Gigabit support (GMII and PCS interface)
18  *  MIF link up/down detection works
19  *
20  * RX is handled by page sized buffers that are attached as fragments to
21  * the skb. here's what's done:
22  *  -- driver allocates pages at a time and keeps reference counts
23  *     on them.
24  *  -- the upper protocol layers assume that the header is in the skb
25  *     itself. as a result, cassini will copy a small amount (64 bytes)
26  *     to make them happy.
27  *  -- driver appends the rest of the data pages as frags to skbuffs
28  *     and increments the reference count
29  *  -- on page reclamation, the driver swaps the page with a spare page.
30  *     if that page is still in use, it frees its reference to that page,
31  *     and allocates a new page for use. otherwise, it just recycles the
32  *     page.
33  *
34  * NOTE: cassini can parse the header. however, it's not worth it
35  *       as long as the network stack requires a header copy.
36  *
37  * TX has 4 queues. currently these queues are used in a round-robin
38  * fashion for load balancing. They can also be used for QoS. for that
39  * to work, however, QoS information needs to be exposed down to the driver
40  * level so that subqueues get targeted to particular transmit rings.
41  * alternatively, the queues can be configured via use of the all-purpose
42  * ioctl.
43  *
44  * RX DATA: the rx completion ring has all the info, but the rx desc
45  * ring has all of the data. RX can conceivably come in under multiple
46  * interrupts, but the INT# assignment needs to be set up properly by
47  * the BIOS and conveyed to the driver. PCI BIOSes don't know how to do
48  * that. also, the two descriptor rings are designed to distinguish between
49  * encrypted and non-encrypted packets, but we use them for buffering
50  * instead.
51  *
52  * by default, the selective clear mask is set up to process rx packets.
53  */
54 
55 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56 
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/compiler.h>
61 #include <linux/slab.h>
62 #include <linux/delay.h>
63 #include <linux/init.h>
64 #include <linux/interrupt.h>
65 #include <linux/vmalloc.h>
66 #include <linux/ioport.h>
67 #include <linux/pci.h>
68 #include <linux/mm.h>
69 #include <linux/highmem.h>
70 #include <linux/list.h>
71 #include <linux/dma-mapping.h>
72 
73 #include <linux/netdevice.h>
74 #include <linux/etherdevice.h>
75 #include <linux/skbuff.h>
76 #include <linux/skbuff_ref.h>
77 #include <linux/ethtool.h>
78 #include <linux/crc32.h>
79 #include <linux/random.h>
80 #include <linux/mii.h>
81 #include <linux/ip.h>
82 #include <linux/tcp.h>
83 #include <linux/mutex.h>
84 #include <linux/firmware.h>
85 
86 #include <net/checksum.h>
87 
88 #include <linux/atomic.h>
89 #include <asm/io.h>
90 #include <asm/byteorder.h>
91 #include <linux/uaccess.h>
92 #include <linux/jiffies.h>
93 
94 #define CAS_NCPUS            num_online_cpus()
95 
96 #define cas_skb_release(x)  netif_rx(x)
97 
98 /* select which firmware to use */
99 #define USE_HP_WORKAROUND
100 #define HP_WORKAROUND_DEFAULT /* select which firmware to use as default */
101 #define CAS_HP_ALT_FIRMWARE   cas_prog_null /* alternate firmware */
102 
103 #include "cassini.h"
104 
105 #define USE_TX_COMPWB      /* use completion writeback registers */
106 #define USE_CSMA_CD_PROTO  /* standard CSMA/CD */
107 #define USE_RX_BLANK       /* hw interrupt mitigation */
108 #undef USE_ENTROPY_DEV     /* don't test for entropy device */
109 
110 /* NOTE: these aren't useable unless PCI interrupts can be assigned.
111  * also, we need to make cp->lock finer-grained.
112  */
113 #undef  USE_PCI_INTB
114 #undef  USE_PCI_INTC
115 #undef  USE_PCI_INTD
116 #undef  USE_QOS
117 
118 #undef  USE_VPD_DEBUG       /* debug vpd information if defined */
119 
120 /* rx processing options */
121 #define USE_PAGE_ORDER      /* specify to allocate large rx pages */
122 #define RX_DONT_BATCH  0    /* if 1, don't batch flows */
123 #define RX_COPY_ALWAYS 0    /* if 0, use frags */
124 #define RX_COPY_MIN    64   /* copy a little to make upper layers happy */
125 #undef  RX_COUNT_BUFFERS    /* define to calculate RX buffer stats */
126 
127 #define DRV_MODULE_NAME		"cassini"
128 #define DRV_MODULE_VERSION	"1.6"
129 #define DRV_MODULE_RELDATE	"21 May 2008"
130 
131 #define CAS_DEF_MSG_ENABLE	  \
132 	(NETIF_MSG_DRV		| \
133 	 NETIF_MSG_PROBE	| \
134 	 NETIF_MSG_LINK		| \
135 	 NETIF_MSG_TIMER	| \
136 	 NETIF_MSG_IFDOWN	| \
137 	 NETIF_MSG_IFUP		| \
138 	 NETIF_MSG_RX_ERR	| \
139 	 NETIF_MSG_TX_ERR)
140 
141 /* length of time before we decide the hardware is borked,
142  * and dev->tx_timeout() should be called to fix the problem
143  */
144 #define CAS_TX_TIMEOUT			(HZ)
145 #define CAS_LINK_TIMEOUT                (22*HZ/10)
146 #define CAS_LINK_FAST_TIMEOUT           (1)
147 
148 /* timeout values for state changing. these specify the number
149  * of 10us delays to be used before giving up.
150  */
151 #define STOP_TRIES_PHY 1000
152 #define STOP_TRIES     5000
153 
154 /* specify a minimum frame size to deal with some fifo issues
155  * max mtu == 2 * page size - ethernet header - 64 - swivel =
156  *            2 * page_size - 0x50
157  */
158 #define CAS_MIN_FRAME			97
159 #define CAS_1000MB_MIN_FRAME            255
160 #define CAS_MIN_MTU                     60
161 #define CAS_MAX_MTU                     min(((cp->page_size << 1) - 0x50), 9000)
162 
163 #if 1
164 /*
165  * Eliminate these and use separate atomic counters for each, to
166  * avoid a race condition.
167  */
168 #else
169 #define CAS_RESET_MTU                   1
170 #define CAS_RESET_ALL                   2
171 #define CAS_RESET_SPARE                 3
172 #endif
173 
174 static char version[] =
175 	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
176 
177 static int cassini_debug = -1;	/* -1 == use CAS_DEF_MSG_ENABLE as value */
178 static int link_mode;
179 
180 MODULE_AUTHOR("Adrian Sun <asun@darksunrising.com>");
181 MODULE_DESCRIPTION("Sun Cassini(+) ethernet driver");
182 MODULE_LICENSE("GPL");
183 MODULE_FIRMWARE("sun/cassini.bin");
184 module_param(cassini_debug, int, 0);
185 MODULE_PARM_DESC(cassini_debug, "Cassini bitmapped debugging message enable value");
186 module_param(link_mode, int, 0);
187 MODULE_PARM_DESC(link_mode, "default link mode");
188 
189 /*
190  * Work around for a PCS bug in which the link goes down due to the chip
191  * being confused and never showing a link status of "up."
192  */
193 #define DEFAULT_LINKDOWN_TIMEOUT 5
194 /*
195  * Value in seconds, for user input.
196  */
197 static int linkdown_timeout = DEFAULT_LINKDOWN_TIMEOUT;
198 module_param(linkdown_timeout, int, 0);
199 MODULE_PARM_DESC(linkdown_timeout,
200 "min reset interval in sec. for PCS linkdown issue; disabled if not positive");
201 
202 /*
203  * value in 'ticks' (units used by jiffies). Set when we init the
204  * module because 'HZ' in actually a function call on some flavors of
205  * Linux.  This will default to DEFAULT_LINKDOWN_TIMEOUT * HZ.
206  */
207 static int link_transition_timeout;
208 
209 
210 
211 static u16 link_modes[] = {
212 	BMCR_ANENABLE,			 /* 0 : autoneg */
213 	0,				 /* 1 : 10bt half duplex */
214 	BMCR_SPEED100,			 /* 2 : 100bt half duplex */
215 	BMCR_FULLDPLX,			 /* 3 : 10bt full duplex */
216 	BMCR_SPEED100|BMCR_FULLDPLX,	 /* 4 : 100bt full duplex */
217 	CAS_BMCR_SPEED1000|BMCR_FULLDPLX /* 5 : 1000bt full duplex */
218 };
219 
220 static const struct pci_device_id cas_pci_tbl[] = {
221 	{ PCI_VDEVICE(SUN, PCI_DEVICE_ID_SUN_CASSINI) },
222 	{ PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SATURN) },
223 	{ }
224 };
225 
226 MODULE_DEVICE_TABLE(pci, cas_pci_tbl);
227 
228 static void cas_set_link_modes(struct cas *cp);
229 
230 static inline void cas_lock_tx(struct cas *cp)
231 {
232 	int i;
233 
234 	for (i = 0; i < N_TX_RINGS; i++)
235 		spin_lock_nested(&cp->tx_lock[i], i);
236 }
237 
238 /* WTZ: QA was finding deadlock problems with the previous
239  * versions after long test runs with multiple cards per machine.
240  * See if replacing cas_lock_all with safer versions helps. The
241  * symptoms QA is reporting match those we'd expect if interrupts
242  * aren't being properly restored, and we fixed a previous deadlock
243  * with similar symptoms by using save/restore versions in other
244  * places.
245  */
246 #define cas_lock_all_save(cp, flags) \
247 do { \
248 	struct cas *xxxcp = (cp); \
249 	spin_lock_irqsave(&xxxcp->lock, flags); \
250 	cas_lock_tx(xxxcp); \
251 } while (0)
252 
253 static inline void cas_unlock_tx(struct cas *cp)
254 {
255 	int i;
256 
257 	for (i = N_TX_RINGS; i > 0; i--)
258 		spin_unlock(&cp->tx_lock[i - 1]);
259 }
260 
261 #define cas_unlock_all_restore(cp, flags) \
262 do { \
263 	struct cas *xxxcp = (cp); \
264 	cas_unlock_tx(xxxcp); \
265 	spin_unlock_irqrestore(&xxxcp->lock, flags); \
266 } while (0)
267 
268 static void cas_disable_irq(struct cas *cp, const int ring)
269 {
270 	/* Make sure we won't get any more interrupts */
271 	if (ring == 0) {
272 		writel(0xFFFFFFFF, cp->regs + REG_INTR_MASK);
273 		return;
274 	}
275 
276 	/* disable completion interrupts and selectively mask */
277 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
278 		switch (ring) {
279 #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
280 #ifdef USE_PCI_INTB
281 		case 1:
282 #endif
283 #ifdef USE_PCI_INTC
284 		case 2:
285 #endif
286 #ifdef USE_PCI_INTD
287 		case 3:
288 #endif
289 			writel(INTRN_MASK_CLEAR_ALL | INTRN_MASK_RX_EN,
290 			       cp->regs + REG_PLUS_INTRN_MASK(ring));
291 			break;
292 #endif
293 		default:
294 			writel(INTRN_MASK_CLEAR_ALL, cp->regs +
295 			       REG_PLUS_INTRN_MASK(ring));
296 			break;
297 		}
298 	}
299 }
300 
301 static inline void cas_mask_intr(struct cas *cp)
302 {
303 	int i;
304 
305 	for (i = 0; i < N_RX_COMP_RINGS; i++)
306 		cas_disable_irq(cp, i);
307 }
308 
309 static void cas_enable_irq(struct cas *cp, const int ring)
310 {
311 	if (ring == 0) { /* all but TX_DONE */
312 		writel(INTR_TX_DONE, cp->regs + REG_INTR_MASK);
313 		return;
314 	}
315 
316 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
317 		switch (ring) {
318 #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
319 #ifdef USE_PCI_INTB
320 		case 1:
321 #endif
322 #ifdef USE_PCI_INTC
323 		case 2:
324 #endif
325 #ifdef USE_PCI_INTD
326 		case 3:
327 #endif
328 			writel(INTRN_MASK_RX_EN, cp->regs +
329 			       REG_PLUS_INTRN_MASK(ring));
330 			break;
331 #endif
332 		default:
333 			break;
334 		}
335 	}
336 }
337 
338 static inline void cas_unmask_intr(struct cas *cp)
339 {
340 	int i;
341 
342 	for (i = 0; i < N_RX_COMP_RINGS; i++)
343 		cas_enable_irq(cp, i);
344 }
345 
346 static inline void cas_entropy_gather(struct cas *cp)
347 {
348 #ifdef USE_ENTROPY_DEV
349 	if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
350 		return;
351 
352 	batch_entropy_store(readl(cp->regs + REG_ENTROPY_IV),
353 			    readl(cp->regs + REG_ENTROPY_IV),
354 			    sizeof(uint64_t)*8);
355 #endif
356 }
357 
358 static inline void cas_entropy_reset(struct cas *cp)
359 {
360 #ifdef USE_ENTROPY_DEV
361 	if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
362 		return;
363 
364 	writel(BIM_LOCAL_DEV_PAD | BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_EXT,
365 	       cp->regs + REG_BIM_LOCAL_DEV_EN);
366 	writeb(ENTROPY_RESET_STC_MODE, cp->regs + REG_ENTROPY_RESET);
367 	writeb(0x55, cp->regs + REG_ENTROPY_RAND_REG);
368 
369 	/* if we read back 0x0, we don't have an entropy device */
370 	if (readb(cp->regs + REG_ENTROPY_RAND_REG) == 0)
371 		cp->cas_flags &= ~CAS_FLAG_ENTROPY_DEV;
372 #endif
373 }
374 
375 /* access to the phy. the following assumes that we've initialized the MIF to
376  * be in frame rather than bit-bang mode
377  */
378 static u16 cas_phy_read(struct cas *cp, int reg)
379 {
380 	u32 cmd;
381 	int limit = STOP_TRIES_PHY;
382 
383 	cmd = MIF_FRAME_ST | MIF_FRAME_OP_READ;
384 	cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
385 	cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
386 	cmd |= MIF_FRAME_TURN_AROUND_MSB;
387 	writel(cmd, cp->regs + REG_MIF_FRAME);
388 
389 	/* poll for completion */
390 	while (limit-- > 0) {
391 		udelay(10);
392 		cmd = readl(cp->regs + REG_MIF_FRAME);
393 		if (cmd & MIF_FRAME_TURN_AROUND_LSB)
394 			return cmd & MIF_FRAME_DATA_MASK;
395 	}
396 	return 0xFFFF; /* -1 */
397 }
398 
399 static int cas_phy_write(struct cas *cp, int reg, u16 val)
400 {
401 	int limit = STOP_TRIES_PHY;
402 	u32 cmd;
403 
404 	cmd = MIF_FRAME_ST | MIF_FRAME_OP_WRITE;
405 	cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
406 	cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
407 	cmd |= MIF_FRAME_TURN_AROUND_MSB;
408 	cmd |= val & MIF_FRAME_DATA_MASK;
409 	writel(cmd, cp->regs + REG_MIF_FRAME);
410 
411 	/* poll for completion */
412 	while (limit-- > 0) {
413 		udelay(10);
414 		cmd = readl(cp->regs + REG_MIF_FRAME);
415 		if (cmd & MIF_FRAME_TURN_AROUND_LSB)
416 			return 0;
417 	}
418 	return -1;
419 }
420 
421 static void cas_phy_powerup(struct cas *cp)
422 {
423 	u16 ctl = cas_phy_read(cp, MII_BMCR);
424 
425 	if ((ctl & BMCR_PDOWN) == 0)
426 		return;
427 	ctl &= ~BMCR_PDOWN;
428 	cas_phy_write(cp, MII_BMCR, ctl);
429 }
430 
431 static void cas_phy_powerdown(struct cas *cp)
432 {
433 	u16 ctl = cas_phy_read(cp, MII_BMCR);
434 
435 	if (ctl & BMCR_PDOWN)
436 		return;
437 	ctl |= BMCR_PDOWN;
438 	cas_phy_write(cp, MII_BMCR, ctl);
439 }
440 
441 /* cp->lock held. note: the last put_page will free the buffer */
442 static int cas_page_free(struct cas *cp, cas_page_t *page)
443 {
444 	dma_unmap_page(&cp->pdev->dev, page->dma_addr, cp->page_size,
445 		       DMA_FROM_DEVICE);
446 	__free_pages(page->buffer, cp->page_order);
447 	kfree(page);
448 	return 0;
449 }
450 
451 #ifdef RX_COUNT_BUFFERS
452 #define RX_USED_ADD(x, y)       ((x)->used += (y))
453 #define RX_USED_SET(x, y)       ((x)->used  = (y))
454 #else
455 #define RX_USED_ADD(x, y) do { } while(0)
456 #define RX_USED_SET(x, y) do { } while(0)
457 #endif
458 
459 /* local page allocation routines for the receive buffers. jumbo pages
460  * require at least 8K contiguous and 8K aligned buffers.
461  */
462 static cas_page_t *cas_page_alloc(struct cas *cp, const gfp_t flags)
463 {
464 	cas_page_t *page;
465 
466 	page = kmalloc_obj(cas_page_t, flags);
467 	if (!page)
468 		return NULL;
469 
470 	INIT_LIST_HEAD(&page->list);
471 	RX_USED_SET(page, 0);
472 	page->buffer = alloc_pages(flags, cp->page_order);
473 	if (!page->buffer)
474 		goto page_err;
475 	page->dma_addr = dma_map_page(&cp->pdev->dev, page->buffer, 0,
476 				      cp->page_size, DMA_FROM_DEVICE);
477 	return page;
478 
479 page_err:
480 	kfree(page);
481 	return NULL;
482 }
483 
484 /* initialize spare pool of rx buffers, but allocate during the open */
485 static void cas_spare_init(struct cas *cp)
486 {
487 	spin_lock(&cp->rx_inuse_lock);
488 	INIT_LIST_HEAD(&cp->rx_inuse_list);
489 	spin_unlock(&cp->rx_inuse_lock);
490 
491 	spin_lock(&cp->rx_spare_lock);
492 	INIT_LIST_HEAD(&cp->rx_spare_list);
493 	cp->rx_spares_needed = RX_SPARE_COUNT;
494 	spin_unlock(&cp->rx_spare_lock);
495 }
496 
497 /* used on close. free all the spare buffers. */
498 static void cas_spare_free(struct cas *cp)
499 {
500 	struct list_head list, *elem, *tmp;
501 
502 	/* free spare buffers */
503 	INIT_LIST_HEAD(&list);
504 	spin_lock(&cp->rx_spare_lock);
505 	list_splice_init(&cp->rx_spare_list, &list);
506 	spin_unlock(&cp->rx_spare_lock);
507 	list_for_each_safe(elem, tmp, &list) {
508 		cas_page_free(cp, list_entry(elem, cas_page_t, list));
509 	}
510 
511 	INIT_LIST_HEAD(&list);
512 #if 1
513 	/*
514 	 * Looks like Adrian had protected this with a different
515 	 * lock than used everywhere else to manipulate this list.
516 	 */
517 	spin_lock(&cp->rx_inuse_lock);
518 	list_splice_init(&cp->rx_inuse_list, &list);
519 	spin_unlock(&cp->rx_inuse_lock);
520 #else
521 	spin_lock(&cp->rx_spare_lock);
522 	list_splice_init(&cp->rx_inuse_list, &list);
523 	spin_unlock(&cp->rx_spare_lock);
524 #endif
525 	list_for_each_safe(elem, tmp, &list) {
526 		cas_page_free(cp, list_entry(elem, cas_page_t, list));
527 	}
528 }
529 
530 /* replenish spares if needed */
531 static void cas_spare_recover(struct cas *cp, const gfp_t flags)
532 {
533 	struct list_head list, *elem, *tmp;
534 	int needed, i;
535 
536 	/* check inuse list. if we don't need any more free buffers,
537 	 * just free it
538 	 */
539 
540 	/* make a local copy of the list */
541 	INIT_LIST_HEAD(&list);
542 	spin_lock(&cp->rx_inuse_lock);
543 	list_splice_init(&cp->rx_inuse_list, &list);
544 	spin_unlock(&cp->rx_inuse_lock);
545 
546 	list_for_each_safe(elem, tmp, &list) {
547 		cas_page_t *page = list_entry(elem, cas_page_t, list);
548 
549 		/*
550 		 * With the lockless pagecache, cassini buffering scheme gets
551 		 * slightly less accurate: we might find that a page has an
552 		 * elevated reference count here, due to a speculative ref,
553 		 * and skip it as in-use. Ideally we would be able to reclaim
554 		 * it. However this would be such a rare case, it doesn't
555 		 * matter too much as we should pick it up the next time round.
556 		 *
557 		 * Importantly, if we find that the page has a refcount of 1
558 		 * here (our refcount), then we know it is definitely not inuse
559 		 * so we can reuse it.
560 		 */
561 		if (page_count(page->buffer) > 1)
562 			continue;
563 
564 		list_del(elem);
565 		spin_lock(&cp->rx_spare_lock);
566 		if (cp->rx_spares_needed > 0) {
567 			list_add(elem, &cp->rx_spare_list);
568 			cp->rx_spares_needed--;
569 			spin_unlock(&cp->rx_spare_lock);
570 		} else {
571 			spin_unlock(&cp->rx_spare_lock);
572 			cas_page_free(cp, page);
573 		}
574 	}
575 
576 	/* put any inuse buffers back on the list */
577 	if (!list_empty(&list)) {
578 		spin_lock(&cp->rx_inuse_lock);
579 		list_splice(&list, &cp->rx_inuse_list);
580 		spin_unlock(&cp->rx_inuse_lock);
581 	}
582 
583 	spin_lock(&cp->rx_spare_lock);
584 	needed = cp->rx_spares_needed;
585 	spin_unlock(&cp->rx_spare_lock);
586 	if (!needed)
587 		return;
588 
589 	/* we still need spares, so try to allocate some */
590 	INIT_LIST_HEAD(&list);
591 	i = 0;
592 	while (i < needed) {
593 		cas_page_t *spare = cas_page_alloc(cp, flags);
594 		if (!spare)
595 			break;
596 		list_add(&spare->list, &list);
597 		i++;
598 	}
599 
600 	spin_lock(&cp->rx_spare_lock);
601 	list_splice(&list, &cp->rx_spare_list);
602 	cp->rx_spares_needed -= i;
603 	spin_unlock(&cp->rx_spare_lock);
604 }
605 
606 /* pull a page from the list. */
607 static cas_page_t *cas_page_dequeue(struct cas *cp)
608 {
609 	struct list_head *entry;
610 	int recover;
611 
612 	spin_lock(&cp->rx_spare_lock);
613 	if (list_empty(&cp->rx_spare_list)) {
614 		/* try to do a quick recovery */
615 		spin_unlock(&cp->rx_spare_lock);
616 		cas_spare_recover(cp, GFP_ATOMIC);
617 		spin_lock(&cp->rx_spare_lock);
618 		if (list_empty(&cp->rx_spare_list)) {
619 			netif_err(cp, rx_err, cp->dev,
620 				  "no spare buffers available\n");
621 			spin_unlock(&cp->rx_spare_lock);
622 			return NULL;
623 		}
624 	}
625 
626 	entry = cp->rx_spare_list.next;
627 	list_del(entry);
628 	recover = ++cp->rx_spares_needed;
629 	spin_unlock(&cp->rx_spare_lock);
630 
631 	/* trigger the timer to do the recovery */
632 	if ((recover & (RX_SPARE_RECOVER_VAL - 1)) == 0) {
633 #if 1
634 		atomic_inc(&cp->reset_task_pending);
635 		atomic_inc(&cp->reset_task_pending_spare);
636 		schedule_work(&cp->reset_task);
637 #else
638 		atomic_set(&cp->reset_task_pending, CAS_RESET_SPARE);
639 		schedule_work(&cp->reset_task);
640 #endif
641 	}
642 	return list_entry(entry, cas_page_t, list);
643 }
644 
645 
646 static void cas_mif_poll(struct cas *cp, const int enable)
647 {
648 	u32 cfg;
649 
650 	cfg  = readl(cp->regs + REG_MIF_CFG);
651 	cfg &= (MIF_CFG_MDIO_0 | MIF_CFG_MDIO_1);
652 
653 	if (cp->phy_type & CAS_PHY_MII_MDIO1)
654 		cfg |= MIF_CFG_PHY_SELECT;
655 
656 	/* poll and interrupt on link status change. */
657 	if (enable) {
658 		cfg |= MIF_CFG_POLL_EN;
659 		cfg |= CAS_BASE(MIF_CFG_POLL_REG, MII_BMSR);
660 		cfg |= CAS_BASE(MIF_CFG_POLL_PHY, cp->phy_addr);
661 	}
662 	writel((enable) ? ~(BMSR_LSTATUS | BMSR_ANEGCOMPLETE) : 0xFFFF,
663 	       cp->regs + REG_MIF_MASK);
664 	writel(cfg, cp->regs + REG_MIF_CFG);
665 }
666 
667 /* Must be invoked under cp->lock */
668 static void cas_begin_auto_negotiation(struct cas *cp,
669 				       const struct ethtool_link_ksettings *ep)
670 {
671 	u16 ctl;
672 #if 1
673 	int lcntl;
674 	int changed = 0;
675 	int oldstate = cp->lstate;
676 	int link_was_not_down = !(oldstate == link_down);
677 #endif
678 	/* Setup link parameters */
679 	if (!ep)
680 		goto start_aneg;
681 	lcntl = cp->link_cntl;
682 	if (ep->base.autoneg == AUTONEG_ENABLE) {
683 		cp->link_cntl = BMCR_ANENABLE;
684 	} else {
685 		u32 speed = ep->base.speed;
686 		cp->link_cntl = 0;
687 		if (speed == SPEED_100)
688 			cp->link_cntl |= BMCR_SPEED100;
689 		else if (speed == SPEED_1000)
690 			cp->link_cntl |= CAS_BMCR_SPEED1000;
691 		if (ep->base.duplex == DUPLEX_FULL)
692 			cp->link_cntl |= BMCR_FULLDPLX;
693 	}
694 #if 1
695 	changed = (lcntl != cp->link_cntl);
696 #endif
697 start_aneg:
698 	if (cp->lstate == link_up) {
699 		netdev_info(cp->dev, "PCS link down\n");
700 	} else {
701 		if (changed) {
702 			netdev_info(cp->dev, "link configuration changed\n");
703 		}
704 	}
705 	cp->lstate = link_down;
706 	cp->link_transition = LINK_TRANSITION_LINK_DOWN;
707 	if (!cp->hw_running)
708 		return;
709 #if 1
710 	/*
711 	 * WTZ: If the old state was link_up, we turn off the carrier
712 	 * to replicate everything we do elsewhere on a link-down
713 	 * event when we were already in a link-up state..
714 	 */
715 	if (oldstate == link_up)
716 		netif_carrier_off(cp->dev);
717 	if (changed  && link_was_not_down) {
718 		/*
719 		 * WTZ: This branch will simply schedule a full reset after
720 		 * we explicitly changed link modes in an ioctl. See if this
721 		 * fixes the link-problems we were having for forced mode.
722 		 */
723 		atomic_inc(&cp->reset_task_pending);
724 		atomic_inc(&cp->reset_task_pending_all);
725 		schedule_work(&cp->reset_task);
726 		cp->timer_ticks = 0;
727 		mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
728 		return;
729 	}
730 #endif
731 	if (cp->phy_type & CAS_PHY_SERDES) {
732 		u32 val = readl(cp->regs + REG_PCS_MII_CTRL);
733 
734 		if (cp->link_cntl & BMCR_ANENABLE) {
735 			val |= (PCS_MII_RESTART_AUTONEG | PCS_MII_AUTONEG_EN);
736 			cp->lstate = link_aneg;
737 		} else {
738 			if (cp->link_cntl & BMCR_FULLDPLX)
739 				val |= PCS_MII_CTRL_DUPLEX;
740 			val &= ~PCS_MII_AUTONEG_EN;
741 			cp->lstate = link_force_ok;
742 		}
743 		cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
744 		writel(val, cp->regs + REG_PCS_MII_CTRL);
745 
746 	} else {
747 		cas_mif_poll(cp, 0);
748 		ctl = cas_phy_read(cp, MII_BMCR);
749 		ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
750 			 CAS_BMCR_SPEED1000 | BMCR_ANENABLE);
751 		ctl |= cp->link_cntl;
752 		if (ctl & BMCR_ANENABLE) {
753 			ctl |= BMCR_ANRESTART;
754 			cp->lstate = link_aneg;
755 		} else {
756 			cp->lstate = link_force_ok;
757 		}
758 		cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
759 		cas_phy_write(cp, MII_BMCR, ctl);
760 		cas_mif_poll(cp, 1);
761 	}
762 
763 	cp->timer_ticks = 0;
764 	mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
765 }
766 
767 /* Must be invoked under cp->lock. */
768 static int cas_reset_mii_phy(struct cas *cp)
769 {
770 	int limit = STOP_TRIES_PHY;
771 	u16 val;
772 
773 	cas_phy_write(cp, MII_BMCR, BMCR_RESET);
774 	udelay(100);
775 	while (--limit) {
776 		val = cas_phy_read(cp, MII_BMCR);
777 		if ((val & BMCR_RESET) == 0)
778 			break;
779 		udelay(10);
780 	}
781 	return limit <= 0;
782 }
783 
784 static void cas_saturn_firmware_init(struct cas *cp)
785 {
786 	const struct firmware *fw;
787 	const char fw_name[] = "sun/cassini.bin";
788 	int err;
789 
790 	if (PHY_NS_DP83065 != cp->phy_id)
791 		return;
792 
793 	err = request_firmware(&fw, fw_name, &cp->pdev->dev);
794 	if (err) {
795 		pr_err("Failed to load firmware \"%s\"\n",
796 		       fw_name);
797 		return;
798 	}
799 	if (fw->size < 2) {
800 		pr_err("bogus length %zu in \"%s\"\n",
801 		       fw->size, fw_name);
802 		goto out;
803 	}
804 	cp->fw_load_addr= fw->data[1] << 8 | fw->data[0];
805 	cp->fw_size = fw->size - 2;
806 	cp->fw_data = vmalloc(cp->fw_size);
807 	if (!cp->fw_data)
808 		goto out;
809 	memcpy(cp->fw_data, &fw->data[2], cp->fw_size);
810 out:
811 	release_firmware(fw);
812 }
813 
814 static void cas_saturn_firmware_load(struct cas *cp)
815 {
816 	int i;
817 
818 	if (!cp->fw_data)
819 		return;
820 
821 	cas_phy_powerdown(cp);
822 
823 	/* expanded memory access mode */
824 	cas_phy_write(cp, DP83065_MII_MEM, 0x0);
825 
826 	/* pointer configuration for new firmware */
827 	cas_phy_write(cp, DP83065_MII_REGE, 0x8ff9);
828 	cas_phy_write(cp, DP83065_MII_REGD, 0xbd);
829 	cas_phy_write(cp, DP83065_MII_REGE, 0x8ffa);
830 	cas_phy_write(cp, DP83065_MII_REGD, 0x82);
831 	cas_phy_write(cp, DP83065_MII_REGE, 0x8ffb);
832 	cas_phy_write(cp, DP83065_MII_REGD, 0x0);
833 	cas_phy_write(cp, DP83065_MII_REGE, 0x8ffc);
834 	cas_phy_write(cp, DP83065_MII_REGD, 0x39);
835 
836 	/* download new firmware */
837 	cas_phy_write(cp, DP83065_MII_MEM, 0x1);
838 	cas_phy_write(cp, DP83065_MII_REGE, cp->fw_load_addr);
839 	for (i = 0; i < cp->fw_size; i++)
840 		cas_phy_write(cp, DP83065_MII_REGD, cp->fw_data[i]);
841 
842 	/* enable firmware */
843 	cas_phy_write(cp, DP83065_MII_REGE, 0x8ff8);
844 	cas_phy_write(cp, DP83065_MII_REGD, 0x1);
845 }
846 
847 
848 /* phy initialization */
849 static void cas_phy_init(struct cas *cp)
850 {
851 	u16 val;
852 
853 	/* if we're in MII/GMII mode, set up phy */
854 	if (CAS_PHY_MII(cp->phy_type)) {
855 		writel(PCS_DATAPATH_MODE_MII,
856 		       cp->regs + REG_PCS_DATAPATH_MODE);
857 
858 		cas_mif_poll(cp, 0);
859 		cas_reset_mii_phy(cp); /* take out of isolate mode */
860 
861 		if (PHY_LUCENT_B0 == cp->phy_id) {
862 			/* workaround link up/down issue with lucent */
863 			cas_phy_write(cp, LUCENT_MII_REG, 0x8000);
864 			cas_phy_write(cp, MII_BMCR, 0x00f1);
865 			cas_phy_write(cp, LUCENT_MII_REG, 0x0);
866 
867 		} else if (PHY_BROADCOM_B0 == (cp->phy_id & 0xFFFFFFFC)) {
868 			/* workarounds for broadcom phy */
869 			cas_phy_write(cp, BROADCOM_MII_REG8, 0x0C20);
870 			cas_phy_write(cp, BROADCOM_MII_REG7, 0x0012);
871 			cas_phy_write(cp, BROADCOM_MII_REG5, 0x1804);
872 			cas_phy_write(cp, BROADCOM_MII_REG7, 0x0013);
873 			cas_phy_write(cp, BROADCOM_MII_REG5, 0x1204);
874 			cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
875 			cas_phy_write(cp, BROADCOM_MII_REG5, 0x0132);
876 			cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
877 			cas_phy_write(cp, BROADCOM_MII_REG5, 0x0232);
878 			cas_phy_write(cp, BROADCOM_MII_REG7, 0x201F);
879 			cas_phy_write(cp, BROADCOM_MII_REG5, 0x0A20);
880 
881 		} else if (PHY_BROADCOM_5411 == cp->phy_id) {
882 			val = cas_phy_read(cp, BROADCOM_MII_REG4);
883 			val = cas_phy_read(cp, BROADCOM_MII_REG4);
884 			if (val & 0x0080) {
885 				/* link workaround */
886 				cas_phy_write(cp, BROADCOM_MII_REG4,
887 					      val & ~0x0080);
888 			}
889 
890 		} else if (cp->cas_flags & CAS_FLAG_SATURN) {
891 			writel((cp->phy_type & CAS_PHY_MII_MDIO0) ?
892 			       SATURN_PCFG_FSI : 0x0,
893 			       cp->regs + REG_SATURN_PCFG);
894 
895 			/* load firmware to address 10Mbps auto-negotiation
896 			 * issue. NOTE: this will need to be changed if the
897 			 * default firmware gets fixed.
898 			 */
899 			if (PHY_NS_DP83065 == cp->phy_id) {
900 				cas_saturn_firmware_load(cp);
901 			}
902 			cas_phy_powerup(cp);
903 		}
904 
905 		/* advertise capabilities */
906 		val = cas_phy_read(cp, MII_BMCR);
907 		val &= ~BMCR_ANENABLE;
908 		cas_phy_write(cp, MII_BMCR, val);
909 		udelay(10);
910 
911 		cas_phy_write(cp, MII_ADVERTISE,
912 			      cas_phy_read(cp, MII_ADVERTISE) |
913 			      (ADVERTISE_10HALF | ADVERTISE_10FULL |
914 			       ADVERTISE_100HALF | ADVERTISE_100FULL |
915 			       CAS_ADVERTISE_PAUSE |
916 			       CAS_ADVERTISE_ASYM_PAUSE));
917 
918 		if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
919 			/* make sure that we don't advertise half
920 			 * duplex to avoid a chip issue
921 			 */
922 			val  = cas_phy_read(cp, CAS_MII_1000_CTRL);
923 			val &= ~CAS_ADVERTISE_1000HALF;
924 			val |= CAS_ADVERTISE_1000FULL;
925 			cas_phy_write(cp, CAS_MII_1000_CTRL, val);
926 		}
927 
928 	} else {
929 		/* reset pcs for serdes */
930 		u32 val;
931 		int limit;
932 
933 		writel(PCS_DATAPATH_MODE_SERDES,
934 		       cp->regs + REG_PCS_DATAPATH_MODE);
935 
936 		/* enable serdes pins on saturn */
937 		if (cp->cas_flags & CAS_FLAG_SATURN)
938 			writel(0, cp->regs + REG_SATURN_PCFG);
939 
940 		/* Reset PCS unit. */
941 		val = readl(cp->regs + REG_PCS_MII_CTRL);
942 		val |= PCS_MII_RESET;
943 		writel(val, cp->regs + REG_PCS_MII_CTRL);
944 
945 		limit = STOP_TRIES;
946 		while (--limit > 0) {
947 			udelay(10);
948 			if ((readl(cp->regs + REG_PCS_MII_CTRL) &
949 			     PCS_MII_RESET) == 0)
950 				break;
951 		}
952 		if (limit <= 0)
953 			netdev_warn(cp->dev, "PCS reset bit would not clear [%08x]\n",
954 				    readl(cp->regs + REG_PCS_STATE_MACHINE));
955 
956 		/* Make sure PCS is disabled while changing advertisement
957 		 * configuration.
958 		 */
959 		writel(0x0, cp->regs + REG_PCS_CFG);
960 
961 		/* Advertise all capabilities except half-duplex. */
962 		val  = readl(cp->regs + REG_PCS_MII_ADVERT);
963 		val &= ~PCS_MII_ADVERT_HD;
964 		val |= (PCS_MII_ADVERT_FD | PCS_MII_ADVERT_SYM_PAUSE |
965 			PCS_MII_ADVERT_ASYM_PAUSE);
966 		writel(val, cp->regs + REG_PCS_MII_ADVERT);
967 
968 		/* enable PCS */
969 		writel(PCS_CFG_EN, cp->regs + REG_PCS_CFG);
970 
971 		/* pcs workaround: enable sync detect */
972 		writel(PCS_SERDES_CTRL_SYNCD_EN,
973 		       cp->regs + REG_PCS_SERDES_CTRL);
974 	}
975 }
976 
977 
978 static int cas_pcs_link_check(struct cas *cp)
979 {
980 	u32 stat, state_machine;
981 	int retval = 0;
982 
983 	/* The link status bit latches on zero, so you must
984 	 * read it twice in such a case to see a transition
985 	 * to the link being up.
986 	 */
987 	stat = readl(cp->regs + REG_PCS_MII_STATUS);
988 	if ((stat & PCS_MII_STATUS_LINK_STATUS) == 0)
989 		stat = readl(cp->regs + REG_PCS_MII_STATUS);
990 
991 	/* The remote-fault indication is only valid
992 	 * when autoneg has completed.
993 	 */
994 	if ((stat & (PCS_MII_STATUS_AUTONEG_COMP |
995 		     PCS_MII_STATUS_REMOTE_FAULT)) ==
996 	    (PCS_MII_STATUS_AUTONEG_COMP | PCS_MII_STATUS_REMOTE_FAULT))
997 		netif_info(cp, link, cp->dev, "PCS RemoteFault\n");
998 
999 	/* work around link detection issue by querying the PCS state
1000 	 * machine directly.
1001 	 */
1002 	state_machine = readl(cp->regs + REG_PCS_STATE_MACHINE);
1003 	if ((state_machine & PCS_SM_LINK_STATE_MASK) != SM_LINK_STATE_UP) {
1004 		stat &= ~PCS_MII_STATUS_LINK_STATUS;
1005 	} else if (state_machine & PCS_SM_WORD_SYNC_STATE_MASK) {
1006 		stat |= PCS_MII_STATUS_LINK_STATUS;
1007 	}
1008 
1009 	if (stat & PCS_MII_STATUS_LINK_STATUS) {
1010 		if (cp->lstate != link_up) {
1011 			if (cp->opened) {
1012 				cp->lstate = link_up;
1013 				cp->link_transition = LINK_TRANSITION_LINK_UP;
1014 
1015 				cas_set_link_modes(cp);
1016 				netif_carrier_on(cp->dev);
1017 			}
1018 		}
1019 	} else if (cp->lstate == link_up) {
1020 		cp->lstate = link_down;
1021 		if (link_transition_timeout != 0 &&
1022 		    cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1023 		    !cp->link_transition_jiffies_valid) {
1024 			/*
1025 			 * force a reset, as a workaround for the
1026 			 * link-failure problem. May want to move this to a
1027 			 * point a bit earlier in the sequence. If we had
1028 			 * generated a reset a short time ago, we'll wait for
1029 			 * the link timer to check the status until a
1030 			 * timer expires (link_transistion_jiffies_valid is
1031 			 * true when the timer is running.)  Instead of using
1032 			 * a system timer, we just do a check whenever the
1033 			 * link timer is running - this clears the flag after
1034 			 * a suitable delay.
1035 			 */
1036 			retval = 1;
1037 			cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1038 			cp->link_transition_jiffies = jiffies;
1039 			cp->link_transition_jiffies_valid = 1;
1040 		} else {
1041 			cp->link_transition = LINK_TRANSITION_ON_FAILURE;
1042 		}
1043 		netif_carrier_off(cp->dev);
1044 		if (cp->opened)
1045 			netif_info(cp, link, cp->dev, "PCS link down\n");
1046 
1047 		/* Cassini only: if you force a mode, there can be
1048 		 * sync problems on link down. to fix that, the following
1049 		 * things need to be checked:
1050 		 * 1) read serialink state register
1051 		 * 2) read pcs status register to verify link down.
1052 		 * 3) if link down and serial link == 0x03, then you need
1053 		 *    to global reset the chip.
1054 		 */
1055 		if ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0) {
1056 			/* should check to see if we're in a forced mode */
1057 			stat = readl(cp->regs + REG_PCS_SERDES_STATE);
1058 			if (stat == 0x03)
1059 				return 1;
1060 		}
1061 	} else if (cp->lstate == link_down) {
1062 		if (link_transition_timeout != 0 &&
1063 		    cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1064 		    !cp->link_transition_jiffies_valid) {
1065 			/* force a reset, as a workaround for the
1066 			 * link-failure problem.  May want to move
1067 			 * this to a point a bit earlier in the
1068 			 * sequence.
1069 			 */
1070 			retval = 1;
1071 			cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1072 			cp->link_transition_jiffies = jiffies;
1073 			cp->link_transition_jiffies_valid = 1;
1074 		} else {
1075 			cp->link_transition = LINK_TRANSITION_STILL_FAILED;
1076 		}
1077 	}
1078 
1079 	return retval;
1080 }
1081 
1082 static int cas_pcs_interrupt(struct net_device *dev,
1083 			     struct cas *cp, u32 status)
1084 {
1085 	u32 stat = readl(cp->regs + REG_PCS_INTR_STATUS);
1086 
1087 	if ((stat & PCS_INTR_STATUS_LINK_CHANGE) == 0)
1088 		return 0;
1089 	return cas_pcs_link_check(cp);
1090 }
1091 
1092 static int cas_txmac_interrupt(struct net_device *dev,
1093 			       struct cas *cp, u32 status)
1094 {
1095 	u32 txmac_stat = readl(cp->regs + REG_MAC_TX_STATUS);
1096 
1097 	if (!txmac_stat)
1098 		return 0;
1099 
1100 	netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1101 		     "txmac interrupt, txmac_stat: 0x%x\n", txmac_stat);
1102 
1103 	/* Defer timer expiration is quite normal,
1104 	 * don't even log the event.
1105 	 */
1106 	if ((txmac_stat & MAC_TX_DEFER_TIMER) &&
1107 	    !(txmac_stat & ~MAC_TX_DEFER_TIMER))
1108 		return 0;
1109 
1110 	spin_lock(&cp->stat_lock[0]);
1111 	if (txmac_stat & MAC_TX_UNDERRUN) {
1112 		netdev_err(dev, "TX MAC xmit underrun\n");
1113 		cp->net_stats[0].tx_fifo_errors++;
1114 	}
1115 
1116 	if (txmac_stat & MAC_TX_MAX_PACKET_ERR) {
1117 		netdev_err(dev, "TX MAC max packet size error\n");
1118 		cp->net_stats[0].tx_errors++;
1119 	}
1120 
1121 	/* The rest are all cases of one of the 16-bit TX
1122 	 * counters expiring.
1123 	 */
1124 	if (txmac_stat & MAC_TX_COLL_NORMAL)
1125 		cp->net_stats[0].collisions += 0x10000;
1126 
1127 	if (txmac_stat & MAC_TX_COLL_EXCESS) {
1128 		cp->net_stats[0].tx_aborted_errors += 0x10000;
1129 		cp->net_stats[0].collisions += 0x10000;
1130 	}
1131 
1132 	if (txmac_stat & MAC_TX_COLL_LATE) {
1133 		cp->net_stats[0].tx_aborted_errors += 0x10000;
1134 		cp->net_stats[0].collisions += 0x10000;
1135 	}
1136 	spin_unlock(&cp->stat_lock[0]);
1137 
1138 	/* We do not keep track of MAC_TX_COLL_FIRST and
1139 	 * MAC_TX_PEAK_ATTEMPTS events.
1140 	 */
1141 	return 0;
1142 }
1143 
1144 static void cas_load_firmware(struct cas *cp, cas_hp_inst_t *firmware)
1145 {
1146 	cas_hp_inst_t *inst;
1147 	u32 val;
1148 	int i;
1149 
1150 	i = 0;
1151 	while ((inst = firmware) && inst->note) {
1152 		writel(i, cp->regs + REG_HP_INSTR_RAM_ADDR);
1153 
1154 		val = CAS_BASE(HP_INSTR_RAM_HI_VAL, inst->val);
1155 		val |= CAS_BASE(HP_INSTR_RAM_HI_MASK, inst->mask);
1156 		writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_HI);
1157 
1158 		val = CAS_BASE(HP_INSTR_RAM_MID_OUTARG, inst->outarg >> 10);
1159 		val |= CAS_BASE(HP_INSTR_RAM_MID_OUTOP, inst->outop);
1160 		val |= CAS_BASE(HP_INSTR_RAM_MID_FNEXT, inst->fnext);
1161 		val |= CAS_BASE(HP_INSTR_RAM_MID_FOFF, inst->foff);
1162 		val |= CAS_BASE(HP_INSTR_RAM_MID_SNEXT, inst->snext);
1163 		val |= CAS_BASE(HP_INSTR_RAM_MID_SOFF, inst->soff);
1164 		val |= CAS_BASE(HP_INSTR_RAM_MID_OP, inst->op);
1165 		writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_MID);
1166 
1167 		val = CAS_BASE(HP_INSTR_RAM_LOW_OUTMASK, inst->outmask);
1168 		val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTSHIFT, inst->outshift);
1169 		val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTEN, inst->outenab);
1170 		val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTARG, inst->outarg);
1171 		writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_LOW);
1172 		++firmware;
1173 		++i;
1174 	}
1175 }
1176 
1177 static void cas_init_rx_dma(struct cas *cp)
1178 {
1179 	u64 desc_dma = cp->block_dvma;
1180 	u32 val;
1181 	int i, size;
1182 
1183 	/* rx free descriptors */
1184 	val = CAS_BASE(RX_CFG_SWIVEL, RX_SWIVEL_OFF_VAL);
1185 	val |= CAS_BASE(RX_CFG_DESC_RING, RX_DESC_RINGN_INDEX(0));
1186 	val |= CAS_BASE(RX_CFG_COMP_RING, RX_COMP_RINGN_INDEX(0));
1187 	if ((N_RX_DESC_RINGS > 1) &&
1188 	    (cp->cas_flags & CAS_FLAG_REG_PLUS))  /* do desc 2 */
1189 		val |= CAS_BASE(RX_CFG_DESC_RING1, RX_DESC_RINGN_INDEX(1));
1190 	writel(val, cp->regs + REG_RX_CFG);
1191 
1192 	val = (unsigned long) cp->init_rxds[0] -
1193 		(unsigned long) cp->init_block;
1194 	writel((desc_dma + val) >> 32, cp->regs + REG_RX_DB_HI);
1195 	writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_DB_LOW);
1196 	writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
1197 
1198 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1199 		/* rx desc 2 is for IPSEC packets. however,
1200 		 * we don't it that for that purpose.
1201 		 */
1202 		val = (unsigned long) cp->init_rxds[1] -
1203 			(unsigned long) cp->init_block;
1204 		writel((desc_dma + val) >> 32, cp->regs + REG_PLUS_RX_DB1_HI);
1205 		writel((desc_dma + val) & 0xffffffff, cp->regs +
1206 		       REG_PLUS_RX_DB1_LOW);
1207 		writel(RX_DESC_RINGN_SIZE(1) - 4, cp->regs +
1208 		       REG_PLUS_RX_KICK1);
1209 	}
1210 
1211 	/* rx completion registers */
1212 	val = (unsigned long) cp->init_rxcs[0] -
1213 		(unsigned long) cp->init_block;
1214 	writel((desc_dma + val) >> 32, cp->regs + REG_RX_CB_HI);
1215 	writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_CB_LOW);
1216 
1217 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1218 		/* rx comp 2-4 */
1219 		for (i = 1; i < MAX_RX_COMP_RINGS; i++) {
1220 			val = (unsigned long) cp->init_rxcs[i] -
1221 				(unsigned long) cp->init_block;
1222 			writel((desc_dma + val) >> 32, cp->regs +
1223 			       REG_PLUS_RX_CBN_HI(i));
1224 			writel((desc_dma + val) & 0xffffffff, cp->regs +
1225 			       REG_PLUS_RX_CBN_LOW(i));
1226 		}
1227 	}
1228 
1229 	/* read selective clear regs to prevent spurious interrupts
1230 	 * on reset because complete == kick.
1231 	 * selective clear set up to prevent interrupts on resets
1232 	 */
1233 	readl(cp->regs + REG_INTR_STATUS_ALIAS);
1234 	writel(INTR_RX_DONE | INTR_RX_BUF_UNAVAIL, cp->regs + REG_ALIAS_CLEAR);
1235 
1236 	/* set up pause thresholds */
1237 	val  = CAS_BASE(RX_PAUSE_THRESH_OFF,
1238 			cp->rx_pause_off / RX_PAUSE_THRESH_QUANTUM);
1239 	val |= CAS_BASE(RX_PAUSE_THRESH_ON,
1240 			cp->rx_pause_on / RX_PAUSE_THRESH_QUANTUM);
1241 	writel(val, cp->regs + REG_RX_PAUSE_THRESH);
1242 
1243 	/* zero out dma reassembly buffers */
1244 	for (i = 0; i < 64; i++) {
1245 		writel(i, cp->regs + REG_RX_TABLE_ADDR);
1246 		writel(0x0, cp->regs + REG_RX_TABLE_DATA_LOW);
1247 		writel(0x0, cp->regs + REG_RX_TABLE_DATA_MID);
1248 		writel(0x0, cp->regs + REG_RX_TABLE_DATA_HI);
1249 	}
1250 
1251 	/* make sure address register is 0 for normal operation */
1252 	writel(0x0, cp->regs + REG_RX_CTRL_FIFO_ADDR);
1253 	writel(0x0, cp->regs + REG_RX_IPP_FIFO_ADDR);
1254 
1255 	/* interrupt mitigation */
1256 #ifdef USE_RX_BLANK
1257 	val = CAS_BASE(RX_BLANK_INTR_TIME, RX_BLANK_INTR_TIME_VAL);
1258 	val |= CAS_BASE(RX_BLANK_INTR_PKT, RX_BLANK_INTR_PKT_VAL);
1259 	writel(val, cp->regs + REG_RX_BLANK);
1260 #else
1261 	writel(0x0, cp->regs + REG_RX_BLANK);
1262 #endif
1263 
1264 	/* interrupt generation as a function of low water marks for
1265 	 * free desc and completion entries. these are used to trigger
1266 	 * housekeeping for rx descs. we don't use the free interrupt
1267 	 * as it's not very useful
1268 	 */
1269 	/* val = CAS_BASE(RX_AE_THRESH_FREE, RX_AE_FREEN_VAL(0)); */
1270 	val = CAS_BASE(RX_AE_THRESH_COMP, RX_AE_COMP_VAL);
1271 	writel(val, cp->regs + REG_RX_AE_THRESH);
1272 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1273 		val = CAS_BASE(RX_AE1_THRESH_FREE, RX_AE_FREEN_VAL(1));
1274 		writel(val, cp->regs + REG_PLUS_RX_AE1_THRESH);
1275 	}
1276 
1277 	/* Random early detect registers. useful for congestion avoidance.
1278 	 * this should be tunable.
1279 	 */
1280 	writel(0x0, cp->regs + REG_RX_RED);
1281 
1282 	/* receive page sizes. default == 2K (0x800) */
1283 	val = 0;
1284 	if (cp->page_size == 0x1000)
1285 		val = 0x1;
1286 	else if (cp->page_size == 0x2000)
1287 		val = 0x2;
1288 	else if (cp->page_size == 0x4000)
1289 		val = 0x3;
1290 
1291 	/* round mtu + offset. constrain to page size. */
1292 	size = cp->dev->mtu + 64;
1293 	if (size > cp->page_size)
1294 		size = cp->page_size;
1295 
1296 	if (size <= 0x400)
1297 		i = 0x0;
1298 	else if (size <= 0x800)
1299 		i = 0x1;
1300 	else if (size <= 0x1000)
1301 		i = 0x2;
1302 	else
1303 		i = 0x3;
1304 
1305 	cp->mtu_stride = 1 << (i + 10);
1306 	val  = CAS_BASE(RX_PAGE_SIZE, val);
1307 	val |= CAS_BASE(RX_PAGE_SIZE_MTU_STRIDE, i);
1308 	val |= CAS_BASE(RX_PAGE_SIZE_MTU_COUNT, cp->page_size >> (i + 10));
1309 	val |= CAS_BASE(RX_PAGE_SIZE_MTU_OFF, 0x1);
1310 	writel(val, cp->regs + REG_RX_PAGE_SIZE);
1311 
1312 	/* enable the header parser if desired */
1313 	if (&CAS_HP_FIRMWARE[0] == &cas_prog_null[0])
1314 		return;
1315 
1316 	val = CAS_BASE(HP_CFG_NUM_CPU, CAS_NCPUS > 63 ? 0 : CAS_NCPUS);
1317 	val |= HP_CFG_PARSE_EN | HP_CFG_SYN_INC_MASK;
1318 	val |= CAS_BASE(HP_CFG_TCP_THRESH, HP_TCP_THRESH_VAL);
1319 	writel(val, cp->regs + REG_HP_CFG);
1320 }
1321 
1322 static inline void cas_rxc_init(struct cas_rx_comp *rxc)
1323 {
1324 	memset(rxc, 0, sizeof(*rxc));
1325 	rxc->word4 = cpu_to_le64(RX_COMP4_ZERO);
1326 }
1327 
1328 /* NOTE: we use the ENC RX DESC ring for spares. the rx_page[0,1]
1329  * flipping is protected by the fact that the chip will not
1330  * hand back the same page index while it's being processed.
1331  */
1332 static inline cas_page_t *cas_page_spare(struct cas *cp, const int index)
1333 {
1334 	cas_page_t *page = cp->rx_pages[1][index];
1335 	cas_page_t *new;
1336 
1337 	if (page_count(page->buffer) == 1)
1338 		return page;
1339 
1340 	new = cas_page_dequeue(cp);
1341 	if (new) {
1342 		spin_lock(&cp->rx_inuse_lock);
1343 		list_add(&page->list, &cp->rx_inuse_list);
1344 		spin_unlock(&cp->rx_inuse_lock);
1345 	}
1346 	return new;
1347 }
1348 
1349 /* this needs to be changed if we actually use the ENC RX DESC ring */
1350 static cas_page_t *cas_page_swap(struct cas *cp, const int ring,
1351 				 const int index)
1352 {
1353 	cas_page_t **page0 = cp->rx_pages[0];
1354 	cas_page_t **page1 = cp->rx_pages[1];
1355 
1356 	/* swap if buffer is in use */
1357 	if (page_count(page0[index]->buffer) > 1) {
1358 		cas_page_t *new = cas_page_spare(cp, index);
1359 		if (new) {
1360 			page1[index] = page0[index];
1361 			page0[index] = new;
1362 		}
1363 	}
1364 	RX_USED_SET(page0[index], 0);
1365 	return page0[index];
1366 }
1367 
1368 static void cas_clean_rxds(struct cas *cp)
1369 {
1370 	/* only clean ring 0 as ring 1 is used for spare buffers */
1371         struct cas_rx_desc *rxd = cp->init_rxds[0];
1372 	int i, size;
1373 
1374 	/* release all rx flows */
1375 	for (i = 0; i < N_RX_FLOWS; i++) {
1376 		struct sk_buff *skb;
1377 		while ((skb = __skb_dequeue(&cp->rx_flows[i]))) {
1378 			cas_skb_release(skb);
1379 		}
1380 	}
1381 
1382 	/* initialize descriptors */
1383 	size = RX_DESC_RINGN_SIZE(0);
1384 	for (i = 0; i < size; i++) {
1385 		cas_page_t *page = cas_page_swap(cp, 0, i);
1386 		rxd[i].buffer = cpu_to_le64(page->dma_addr);
1387 		rxd[i].index  = cpu_to_le64(CAS_BASE(RX_INDEX_NUM, i) |
1388 					    CAS_BASE(RX_INDEX_RING, 0));
1389 	}
1390 
1391 	cp->rx_old[0]  = RX_DESC_RINGN_SIZE(0) - 4;
1392 	cp->rx_last[0] = 0;
1393 	cp->cas_flags &= ~CAS_FLAG_RXD_POST(0);
1394 }
1395 
1396 static void cas_clean_rxcs(struct cas *cp)
1397 {
1398 	int i, j;
1399 
1400 	/* take ownership of rx comp descriptors */
1401 	memset(cp->rx_cur, 0, sizeof(*cp->rx_cur)*N_RX_COMP_RINGS);
1402 	memset(cp->rx_new, 0, sizeof(*cp->rx_new)*N_RX_COMP_RINGS);
1403 	for (i = 0; i < N_RX_COMP_RINGS; i++) {
1404 		struct cas_rx_comp *rxc = cp->init_rxcs[i];
1405 		for (j = 0; j < RX_COMP_RINGN_SIZE(i); j++) {
1406 			cas_rxc_init(rxc + j);
1407 		}
1408 	}
1409 }
1410 
1411 #if 0
1412 /* When we get a RX fifo overflow, the RX unit is probably hung
1413  * so we do the following.
1414  *
1415  * If any part of the reset goes wrong, we return 1 and that causes the
1416  * whole chip to be reset.
1417  */
1418 static int cas_rxmac_reset(struct cas *cp)
1419 {
1420 	struct net_device *dev = cp->dev;
1421 	int limit;
1422 	u32 val;
1423 
1424 	/* First, reset MAC RX. */
1425 	writel(cp->mac_rx_cfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1426 	for (limit = 0; limit < STOP_TRIES; limit++) {
1427 		if (!(readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN))
1428 			break;
1429 		udelay(10);
1430 	}
1431 	if (limit == STOP_TRIES) {
1432 		netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
1433 		return 1;
1434 	}
1435 
1436 	/* Second, disable RX DMA. */
1437 	writel(0, cp->regs + REG_RX_CFG);
1438 	for (limit = 0; limit < STOP_TRIES; limit++) {
1439 		if (!(readl(cp->regs + REG_RX_CFG) & RX_CFG_DMA_EN))
1440 			break;
1441 		udelay(10);
1442 	}
1443 	if (limit == STOP_TRIES) {
1444 		netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
1445 		return 1;
1446 	}
1447 
1448 	mdelay(5);
1449 
1450 	/* Execute RX reset command. */
1451 	writel(SW_RESET_RX, cp->regs + REG_SW_RESET);
1452 	for (limit = 0; limit < STOP_TRIES; limit++) {
1453 		if (!(readl(cp->regs + REG_SW_RESET) & SW_RESET_RX))
1454 			break;
1455 		udelay(10);
1456 	}
1457 	if (limit == STOP_TRIES) {
1458 		netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
1459 		return 1;
1460 	}
1461 
1462 	/* reset driver rx state */
1463 	cas_clean_rxds(cp);
1464 	cas_clean_rxcs(cp);
1465 
1466 	/* Now, reprogram the rest of RX unit. */
1467 	cas_init_rx_dma(cp);
1468 
1469 	/* re-enable */
1470 	val = readl(cp->regs + REG_RX_CFG);
1471 	writel(val | RX_CFG_DMA_EN, cp->regs + REG_RX_CFG);
1472 	writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
1473 	val = readl(cp->regs + REG_MAC_RX_CFG);
1474 	writel(val | MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1475 	return 0;
1476 }
1477 #endif
1478 
1479 static int cas_rxmac_interrupt(struct net_device *dev, struct cas *cp,
1480 			       u32 status)
1481 {
1482 	u32 stat = readl(cp->regs + REG_MAC_RX_STATUS);
1483 
1484 	if (!stat)
1485 		return 0;
1486 
1487 	netif_dbg(cp, intr, cp->dev, "rxmac interrupt, stat: 0x%x\n", stat);
1488 
1489 	/* these are all rollovers */
1490 	spin_lock(&cp->stat_lock[0]);
1491 	if (stat & MAC_RX_ALIGN_ERR)
1492 		cp->net_stats[0].rx_frame_errors += 0x10000;
1493 
1494 	if (stat & MAC_RX_CRC_ERR)
1495 		cp->net_stats[0].rx_crc_errors += 0x10000;
1496 
1497 	if (stat & MAC_RX_LEN_ERR)
1498 		cp->net_stats[0].rx_length_errors += 0x10000;
1499 
1500 	if (stat & MAC_RX_OVERFLOW) {
1501 		cp->net_stats[0].rx_over_errors++;
1502 		cp->net_stats[0].rx_fifo_errors++;
1503 	}
1504 
1505 	/* We do not track MAC_RX_FRAME_COUNT and MAC_RX_VIOL_ERR
1506 	 * events.
1507 	 */
1508 	spin_unlock(&cp->stat_lock[0]);
1509 	return 0;
1510 }
1511 
1512 static int cas_mac_interrupt(struct net_device *dev, struct cas *cp,
1513 			     u32 status)
1514 {
1515 	u32 stat = readl(cp->regs + REG_MAC_CTRL_STATUS);
1516 
1517 	if (!stat)
1518 		return 0;
1519 
1520 	netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1521 		     "mac interrupt, stat: 0x%x\n", stat);
1522 
1523 	/* This interrupt is just for pause frame and pause
1524 	 * tracking.  It is useful for diagnostics and debug
1525 	 * but probably by default we will mask these events.
1526 	 */
1527 	if (stat & MAC_CTRL_PAUSE_STATE)
1528 		cp->pause_entered++;
1529 
1530 	if (stat & MAC_CTRL_PAUSE_RECEIVED)
1531 		cp->pause_last_time_recvd = (stat >> 16);
1532 
1533 	return 0;
1534 }
1535 
1536 
1537 /* Must be invoked under cp->lock. */
1538 static inline int cas_mdio_link_not_up(struct cas *cp)
1539 {
1540 	u16 val;
1541 
1542 	switch (cp->lstate) {
1543 	case link_force_ret:
1544 		netif_info(cp, link, cp->dev, "Autoneg failed again, keeping forced mode\n");
1545 		cas_phy_write(cp, MII_BMCR, cp->link_fcntl);
1546 		cp->timer_ticks = 5;
1547 		cp->lstate = link_force_ok;
1548 		cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1549 		break;
1550 
1551 	case link_aneg:
1552 		val = cas_phy_read(cp, MII_BMCR);
1553 
1554 		/* Try forced modes. we try things in the following order:
1555 		 * 1000 full -> 100 full/half -> 10 half
1556 		 */
1557 		val &= ~(BMCR_ANRESTART | BMCR_ANENABLE);
1558 		val |= BMCR_FULLDPLX;
1559 		val |= (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
1560 			CAS_BMCR_SPEED1000 : BMCR_SPEED100;
1561 		cas_phy_write(cp, MII_BMCR, val);
1562 		cp->timer_ticks = 5;
1563 		cp->lstate = link_force_try;
1564 		cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1565 		break;
1566 
1567 	case link_force_try:
1568 		/* Downgrade from 1000 to 100 to 10 Mbps if necessary. */
1569 		val = cas_phy_read(cp, MII_BMCR);
1570 		cp->timer_ticks = 5;
1571 		if (val & CAS_BMCR_SPEED1000) { /* gigabit */
1572 			val &= ~CAS_BMCR_SPEED1000;
1573 			val |= (BMCR_SPEED100 | BMCR_FULLDPLX);
1574 			cas_phy_write(cp, MII_BMCR, val);
1575 			break;
1576 		}
1577 
1578 		if (val & BMCR_SPEED100) {
1579 			if (val & BMCR_FULLDPLX) /* fd failed */
1580 				val &= ~BMCR_FULLDPLX;
1581 			else { /* 100Mbps failed */
1582 				val &= ~BMCR_SPEED100;
1583 			}
1584 			cas_phy_write(cp, MII_BMCR, val);
1585 			break;
1586 		}
1587 		break;
1588 	default:
1589 		break;
1590 	}
1591 	return 0;
1592 }
1593 
1594 
1595 /* must be invoked with cp->lock held */
1596 static int cas_mii_link_check(struct cas *cp, const u16 bmsr)
1597 {
1598 	int restart;
1599 
1600 	if (bmsr & BMSR_LSTATUS) {
1601 		/* Ok, here we got a link. If we had it due to a forced
1602 		 * fallback, and we were configured for autoneg, we
1603 		 * retry a short autoneg pass. If you know your hub is
1604 		 * broken, use ethtool ;)
1605 		 */
1606 		if ((cp->lstate == link_force_try) &&
1607 		    (cp->link_cntl & BMCR_ANENABLE)) {
1608 			cp->lstate = link_force_ret;
1609 			cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1610 			cas_mif_poll(cp, 0);
1611 			cp->link_fcntl = cas_phy_read(cp, MII_BMCR);
1612 			cp->timer_ticks = 5;
1613 			if (cp->opened)
1614 				netif_info(cp, link, cp->dev,
1615 					   "Got link after fallback, retrying autoneg once...\n");
1616 			cas_phy_write(cp, MII_BMCR,
1617 				      cp->link_fcntl | BMCR_ANENABLE |
1618 				      BMCR_ANRESTART);
1619 			cas_mif_poll(cp, 1);
1620 
1621 		} else if (cp->lstate != link_up) {
1622 			cp->lstate = link_up;
1623 			cp->link_transition = LINK_TRANSITION_LINK_UP;
1624 
1625 			if (cp->opened) {
1626 				cas_set_link_modes(cp);
1627 				netif_carrier_on(cp->dev);
1628 			}
1629 		}
1630 		return 0;
1631 	}
1632 
1633 	/* link not up. if the link was previously up, we restart the
1634 	 * whole process
1635 	 */
1636 	restart = 0;
1637 	if (cp->lstate == link_up) {
1638 		cp->lstate = link_down;
1639 		cp->link_transition = LINK_TRANSITION_LINK_DOWN;
1640 
1641 		netif_carrier_off(cp->dev);
1642 		if (cp->opened)
1643 			netif_info(cp, link, cp->dev, "Link down\n");
1644 		restart = 1;
1645 
1646 	} else if (++cp->timer_ticks > 10)
1647 		cas_mdio_link_not_up(cp);
1648 
1649 	return restart;
1650 }
1651 
1652 static int cas_mif_interrupt(struct net_device *dev, struct cas *cp,
1653 			     u32 status)
1654 {
1655 	u32 stat = readl(cp->regs + REG_MIF_STATUS);
1656 	u16 bmsr;
1657 
1658 	/* check for a link change */
1659 	if (CAS_VAL(MIF_STATUS_POLL_STATUS, stat) == 0)
1660 		return 0;
1661 
1662 	bmsr = CAS_VAL(MIF_STATUS_POLL_DATA, stat);
1663 	return cas_mii_link_check(cp, bmsr);
1664 }
1665 
1666 static int cas_pci_interrupt(struct net_device *dev, struct cas *cp,
1667 			     u32 status)
1668 {
1669 	u32 stat = readl(cp->regs + REG_PCI_ERR_STATUS);
1670 
1671 	if (!stat)
1672 		return 0;
1673 
1674 	netdev_err(dev, "PCI error [%04x:%04x]",
1675 		   stat, readl(cp->regs + REG_BIM_DIAG));
1676 
1677 	/* cassini+ has this reserved */
1678 	if ((stat & PCI_ERR_BADACK) &&
1679 	    ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0))
1680 		pr_cont(" <No ACK64# during ABS64 cycle>");
1681 
1682 	if (stat & PCI_ERR_DTRTO)
1683 		pr_cont(" <Delayed transaction timeout>");
1684 	if (stat & PCI_ERR_OTHER)
1685 		pr_cont(" <other>");
1686 	if (stat & PCI_ERR_BIM_DMA_WRITE)
1687 		pr_cont(" <BIM DMA 0 write req>");
1688 	if (stat & PCI_ERR_BIM_DMA_READ)
1689 		pr_cont(" <BIM DMA 0 read req>");
1690 	pr_cont("\n");
1691 
1692 	if (stat & PCI_ERR_OTHER) {
1693 		int pci_errs;
1694 
1695 		/* Interrogate PCI config space for the
1696 		 * true cause.
1697 		 */
1698 		pci_errs = pci_status_get_and_clear_errors(cp->pdev);
1699 
1700 		netdev_err(dev, "PCI status errors[%04x]\n", pci_errs);
1701 		if (pci_errs & PCI_STATUS_PARITY)
1702 			netdev_err(dev, "PCI parity error detected\n");
1703 		if (pci_errs & PCI_STATUS_SIG_TARGET_ABORT)
1704 			netdev_err(dev, "PCI target abort\n");
1705 		if (pci_errs & PCI_STATUS_REC_TARGET_ABORT)
1706 			netdev_err(dev, "PCI master acks target abort\n");
1707 		if (pci_errs & PCI_STATUS_REC_MASTER_ABORT)
1708 			netdev_err(dev, "PCI master abort\n");
1709 		if (pci_errs & PCI_STATUS_SIG_SYSTEM_ERROR)
1710 			netdev_err(dev, "PCI system error SERR#\n");
1711 		if (pci_errs & PCI_STATUS_DETECTED_PARITY)
1712 			netdev_err(dev, "PCI parity error\n");
1713 	}
1714 
1715 	/* For all PCI errors, we should reset the chip. */
1716 	return 1;
1717 }
1718 
1719 /* All non-normal interrupt conditions get serviced here.
1720  * Returns non-zero if we should just exit the interrupt
1721  * handler right now (ie. if we reset the card which invalidates
1722  * all of the other original irq status bits).
1723  */
1724 static int cas_abnormal_irq(struct net_device *dev, struct cas *cp,
1725 			    u32 status)
1726 {
1727 	if (status & INTR_RX_TAG_ERROR) {
1728 		/* corrupt RX tag framing */
1729 		netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1730 			     "corrupt rx tag framing\n");
1731 		spin_lock(&cp->stat_lock[0]);
1732 		cp->net_stats[0].rx_errors++;
1733 		spin_unlock(&cp->stat_lock[0]);
1734 		goto do_reset;
1735 	}
1736 
1737 	if (status & INTR_RX_LEN_MISMATCH) {
1738 		/* length mismatch. */
1739 		netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1740 			     "length mismatch for rx frame\n");
1741 		spin_lock(&cp->stat_lock[0]);
1742 		cp->net_stats[0].rx_errors++;
1743 		spin_unlock(&cp->stat_lock[0]);
1744 		goto do_reset;
1745 	}
1746 
1747 	if (status & INTR_PCS_STATUS) {
1748 		if (cas_pcs_interrupt(dev, cp, status))
1749 			goto do_reset;
1750 	}
1751 
1752 	if (status & INTR_TX_MAC_STATUS) {
1753 		if (cas_txmac_interrupt(dev, cp, status))
1754 			goto do_reset;
1755 	}
1756 
1757 	if (status & INTR_RX_MAC_STATUS) {
1758 		if (cas_rxmac_interrupt(dev, cp, status))
1759 			goto do_reset;
1760 	}
1761 
1762 	if (status & INTR_MAC_CTRL_STATUS) {
1763 		if (cas_mac_interrupt(dev, cp, status))
1764 			goto do_reset;
1765 	}
1766 
1767 	if (status & INTR_MIF_STATUS) {
1768 		if (cas_mif_interrupt(dev, cp, status))
1769 			goto do_reset;
1770 	}
1771 
1772 	if (status & INTR_PCI_ERROR_STATUS) {
1773 		if (cas_pci_interrupt(dev, cp, status))
1774 			goto do_reset;
1775 	}
1776 	return 0;
1777 
1778 do_reset:
1779 #if 1
1780 	atomic_inc(&cp->reset_task_pending);
1781 	atomic_inc(&cp->reset_task_pending_all);
1782 	netdev_err(dev, "reset called in cas_abnormal_irq [0x%x]\n", status);
1783 	schedule_work(&cp->reset_task);
1784 #else
1785 	atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
1786 	netdev_err(dev, "reset called in cas_abnormal_irq\n");
1787 	schedule_work(&cp->reset_task);
1788 #endif
1789 	return 1;
1790 }
1791 
1792 /* NOTE: CAS_TABORT returns 1 or 2 so that it can be used when
1793  *       determining whether to do a netif_stop/wakeup
1794  */
1795 #define CAS_TABORT(x)      (((x)->cas_flags & CAS_FLAG_TARGET_ABORT) ? 2 : 1)
1796 #define CAS_ROUND_PAGE(x)  (((x) + PAGE_SIZE - 1) & PAGE_MASK)
1797 static inline int cas_calc_tabort(struct cas *cp, const unsigned long addr,
1798 				  const int len)
1799 {
1800 	unsigned long off = addr + len;
1801 
1802 	if (CAS_TABORT(cp) == 1)
1803 		return 0;
1804 	if ((CAS_ROUND_PAGE(off) - off) > TX_TARGET_ABORT_LEN)
1805 		return 0;
1806 	return TX_TARGET_ABORT_LEN;
1807 }
1808 
1809 static inline void cas_tx_ringN(struct cas *cp, int ring, int limit)
1810 {
1811 	struct cas_tx_desc *txds;
1812 	struct sk_buff **skbs;
1813 	struct net_device *dev = cp->dev;
1814 	int entry, count;
1815 
1816 	spin_lock(&cp->tx_lock[ring]);
1817 	txds = cp->init_txds[ring];
1818 	skbs = cp->tx_skbs[ring];
1819 	entry = cp->tx_old[ring];
1820 
1821 	count = TX_BUFF_COUNT(ring, entry, limit);
1822 	while (entry != limit) {
1823 		struct sk_buff *skb = skbs[entry];
1824 		dma_addr_t daddr;
1825 		u32 dlen;
1826 		int frag;
1827 
1828 		if (!skb) {
1829 			/* this should never occur */
1830 			entry = TX_DESC_NEXT(ring, entry);
1831 			continue;
1832 		}
1833 
1834 		/* however, we might get only a partial skb release. */
1835 		count -= skb_shinfo(skb)->nr_frags +
1836 			+ cp->tx_tiny_use[ring][entry].nbufs + 1;
1837 		if (count < 0)
1838 			break;
1839 
1840 		netif_printk(cp, tx_done, KERN_DEBUG, cp->dev,
1841 			     "tx[%d] done, slot %d\n", ring, entry);
1842 
1843 		skbs[entry] = NULL;
1844 		cp->tx_tiny_use[ring][entry].nbufs = 0;
1845 
1846 		for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1847 			struct cas_tx_desc *txd = txds + entry;
1848 
1849 			daddr = le64_to_cpu(txd->buffer);
1850 			dlen = CAS_VAL(TX_DESC_BUFLEN,
1851 				       le64_to_cpu(txd->control));
1852 			dma_unmap_page(&cp->pdev->dev, daddr, dlen,
1853 				       DMA_TO_DEVICE);
1854 			entry = TX_DESC_NEXT(ring, entry);
1855 
1856 			/* tiny buffer may follow */
1857 			if (cp->tx_tiny_use[ring][entry].used) {
1858 				cp->tx_tiny_use[ring][entry].used = 0;
1859 				entry = TX_DESC_NEXT(ring, entry);
1860 			}
1861 		}
1862 
1863 		spin_lock(&cp->stat_lock[ring]);
1864 		cp->net_stats[ring].tx_packets++;
1865 		cp->net_stats[ring].tx_bytes += skb->len;
1866 		spin_unlock(&cp->stat_lock[ring]);
1867 		dev_consume_skb_irq(skb);
1868 	}
1869 	cp->tx_old[ring] = entry;
1870 
1871 	/* this is wrong for multiple tx rings. the net device needs
1872 	 * multiple queues for this to do the right thing.  we wait
1873 	 * for 2*packets to be available when using tiny buffers
1874 	 */
1875 	if (netif_queue_stopped(dev) &&
1876 	    (TX_BUFFS_AVAIL(cp, ring) > CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1)))
1877 		netif_wake_queue(dev);
1878 	spin_unlock(&cp->tx_lock[ring]);
1879 }
1880 
1881 static void cas_tx(struct net_device *dev, struct cas *cp,
1882 		   u32 status)
1883 {
1884         int limit, ring;
1885 #ifdef USE_TX_COMPWB
1886 	u64 compwb = le64_to_cpu(cp->init_block->tx_compwb);
1887 #endif
1888 	netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1889 		     "tx interrupt, status: 0x%x, %llx\n",
1890 		     status, (unsigned long long)compwb);
1891 	/* process all the rings */
1892 	for (ring = 0; ring < N_TX_RINGS; ring++) {
1893 #ifdef USE_TX_COMPWB
1894 		/* use the completion writeback registers */
1895 		limit = (CAS_VAL(TX_COMPWB_MSB, compwb) << 8) |
1896 			CAS_VAL(TX_COMPWB_LSB, compwb);
1897 		compwb = TX_COMPWB_NEXT(compwb);
1898 #else
1899 		limit = readl(cp->regs + REG_TX_COMPN(ring));
1900 #endif
1901 		if (cp->tx_old[ring] != limit)
1902 			cas_tx_ringN(cp, ring, limit);
1903 	}
1904 }
1905 
1906 
1907 static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
1908 			      int entry, const u64 *words,
1909 			      struct sk_buff **skbref)
1910 {
1911 	int dlen, hlen, len, i, alloclen;
1912 	int off, swivel = RX_SWIVEL_OFF_VAL;
1913 	struct cas_page *page;
1914 	struct sk_buff *skb;
1915 	void *crcaddr;
1916 	__sum16 csum;
1917 	char *p;
1918 
1919 	hlen = CAS_VAL(RX_COMP2_HDR_SIZE, words[1]);
1920 	dlen = CAS_VAL(RX_COMP1_DATA_SIZE, words[0]);
1921 	len  = hlen + dlen;
1922 
1923 	if (RX_COPY_ALWAYS || (words[2] & RX_COMP3_SMALL_PKT))
1924 		alloclen = len;
1925 	else
1926 		alloclen = max(hlen, RX_COPY_MIN);
1927 
1928 	skb = netdev_alloc_skb(cp->dev, alloclen + swivel + cp->crc_size);
1929 	if (skb == NULL)
1930 		return -1;
1931 
1932 	*skbref = skb;
1933 	skb_reserve(skb, swivel);
1934 
1935 	p = skb->data;
1936 	crcaddr = NULL;
1937 	if (hlen) { /* always copy header pages */
1938 		i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
1939 		page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
1940 		off = CAS_VAL(RX_COMP2_HDR_OFF, words[1]) * 0x100 +
1941 			swivel;
1942 
1943 		i = hlen;
1944 		if (!dlen) /* attach FCS */
1945 			i += cp->crc_size;
1946 		dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
1947 					i, DMA_FROM_DEVICE);
1948 		memcpy(p, page_address(page->buffer) + off, i);
1949 		dma_sync_single_for_device(&cp->pdev->dev,
1950 					   page->dma_addr + off, i,
1951 					   DMA_FROM_DEVICE);
1952 		RX_USED_ADD(page, 0x100);
1953 		p += hlen;
1954 		swivel = 0;
1955 	}
1956 
1957 
1958 	if (alloclen < (hlen + dlen)) {
1959 		skb_frag_t *frag = skb_shinfo(skb)->frags;
1960 
1961 		/* normal or jumbo packets. we use frags */
1962 		i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
1963 		page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
1964 		off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
1965 
1966 		hlen = min(cp->page_size - off, dlen);
1967 		if (hlen < 0) {
1968 			netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1969 				     "rx page overflow: %d\n", hlen);
1970 			dev_kfree_skb_irq(skb);
1971 			return -1;
1972 		}
1973 		i = hlen;
1974 		if (i == dlen)  /* attach FCS */
1975 			i += cp->crc_size;
1976 		dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
1977 					i, DMA_FROM_DEVICE);
1978 
1979 		/* make sure we always copy a header */
1980 		swivel = 0;
1981 		if (p == (char *) skb->data) { /* not split */
1982 			memcpy(p, page_address(page->buffer) + off,
1983 			       RX_COPY_MIN);
1984 			dma_sync_single_for_device(&cp->pdev->dev,
1985 						   page->dma_addr + off, i,
1986 						   DMA_FROM_DEVICE);
1987 			off += RX_COPY_MIN;
1988 			swivel = RX_COPY_MIN;
1989 			RX_USED_ADD(page, cp->mtu_stride);
1990 		} else {
1991 			RX_USED_ADD(page, hlen);
1992 		}
1993 		skb_put(skb, alloclen);
1994 
1995 		skb_shinfo(skb)->nr_frags++;
1996 		skb->data_len += hlen - swivel;
1997 		skb->truesize += hlen - swivel;
1998 		skb->len      += hlen - swivel;
1999 
2000 		skb_frag_fill_page_desc(frag, page->buffer, off, hlen - swivel);
2001 		__skb_frag_ref(frag);
2002 
2003 		/* any more data? */
2004 		if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2005 			hlen = dlen;
2006 			off = 0;
2007 
2008 			i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2009 			page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2010 			dma_sync_single_for_cpu(&cp->pdev->dev,
2011 						page->dma_addr,
2012 						hlen + cp->crc_size,
2013 						DMA_FROM_DEVICE);
2014 			dma_sync_single_for_device(&cp->pdev->dev,
2015 						   page->dma_addr,
2016 						   hlen + cp->crc_size,
2017 						   DMA_FROM_DEVICE);
2018 
2019 			skb_shinfo(skb)->nr_frags++;
2020 			skb->data_len += hlen;
2021 			skb->len      += hlen;
2022 			frag++;
2023 
2024 			skb_frag_fill_page_desc(frag, page->buffer, 0, hlen);
2025 			__skb_frag_ref(frag);
2026 			RX_USED_ADD(page, hlen + cp->crc_size);
2027 		}
2028 
2029 		if (cp->crc_size)
2030 			crcaddr = page_address(page->buffer) + off + hlen;
2031 
2032 	} else {
2033 		/* copying packet */
2034 		if (!dlen)
2035 			goto end_copy_pkt;
2036 
2037 		i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2038 		page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2039 		off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
2040 		hlen = min(cp->page_size - off, dlen);
2041 		if (hlen < 0) {
2042 			netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
2043 				     "rx page overflow: %d\n", hlen);
2044 			dev_kfree_skb_irq(skb);
2045 			return -1;
2046 		}
2047 		i = hlen;
2048 		if (i == dlen) /* attach FCS */
2049 			i += cp->crc_size;
2050 		dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
2051 					i, DMA_FROM_DEVICE);
2052 		memcpy(p, page_address(page->buffer) + off, i);
2053 		dma_sync_single_for_device(&cp->pdev->dev,
2054 					   page->dma_addr + off, i,
2055 					   DMA_FROM_DEVICE);
2056 		if (p == (char *) skb->data) /* not split */
2057 			RX_USED_ADD(page, cp->mtu_stride);
2058 		else
2059 			RX_USED_ADD(page, i);
2060 
2061 		/* any more data? */
2062 		if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2063 			p += hlen;
2064 			i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2065 			page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2066 			dma_sync_single_for_cpu(&cp->pdev->dev,
2067 						page->dma_addr,
2068 						dlen + cp->crc_size,
2069 						DMA_FROM_DEVICE);
2070 			memcpy(p, page_address(page->buffer), dlen + cp->crc_size);
2071 			dma_sync_single_for_device(&cp->pdev->dev,
2072 						   page->dma_addr,
2073 						   dlen + cp->crc_size,
2074 						   DMA_FROM_DEVICE);
2075 			RX_USED_ADD(page, dlen + cp->crc_size);
2076 		}
2077 end_copy_pkt:
2078 		if (cp->crc_size)
2079 			crcaddr = skb->data + alloclen;
2080 
2081 		skb_put(skb, alloclen);
2082 	}
2083 
2084 	csum = (__force __sum16)htons(CAS_VAL(RX_COMP4_TCP_CSUM, words[3]));
2085 	if (cp->crc_size) {
2086 		/* checksum includes FCS. strip it out. */
2087 		csum = csum_fold(csum_partial(crcaddr, cp->crc_size,
2088 					      csum_unfold(csum)));
2089 	}
2090 	skb->protocol = eth_type_trans(skb, cp->dev);
2091 	if (skb->protocol == htons(ETH_P_IP)) {
2092 		skb->csum = csum_unfold(~csum);
2093 		skb->ip_summed = CHECKSUM_COMPLETE;
2094 	} else
2095 		skb_checksum_none_assert(skb);
2096 	return len;
2097 }
2098 
2099 
2100 /* we can handle up to 64 rx flows at a time. we do the same thing
2101  * as nonreassm except that we batch up the buffers.
2102  * NOTE: we currently just treat each flow as a bunch of packets that
2103  *       we pass up. a better way would be to coalesce the packets
2104  *       into a jumbo packet. to do that, we need to do the following:
2105  *       1) the first packet will have a clean split between header and
2106  *          data. save both.
2107  *       2) each time the next flow packet comes in, extend the
2108  *          data length and merge the checksums.
2109  *       3) on flow release, fix up the header.
2110  *       4) make sure the higher layer doesn't care.
2111  * because packets get coalesced, we shouldn't run into fragment count
2112  * issues.
2113  */
2114 static inline void cas_rx_flow_pkt(struct cas *cp, const u64 *words,
2115 				   struct sk_buff *skb)
2116 {
2117 	int flowid = CAS_VAL(RX_COMP3_FLOWID, words[2]) & (N_RX_FLOWS - 1);
2118 	struct sk_buff_head *flow = &cp->rx_flows[flowid];
2119 
2120 	/* this is protected at a higher layer, so no need to
2121 	 * do any additional locking here. stick the buffer
2122 	 * at the end.
2123 	 */
2124 	__skb_queue_tail(flow, skb);
2125 	if (words[0] & RX_COMP1_RELEASE_FLOW) {
2126 		while ((skb = __skb_dequeue(flow))) {
2127 			cas_skb_release(skb);
2128 		}
2129 	}
2130 }
2131 
2132 /* put rx descriptor back on ring. if a buffer is in use by a higher
2133  * layer, this will need to put in a replacement.
2134  */
2135 static void cas_post_page(struct cas *cp, const int ring, const int index)
2136 {
2137 	cas_page_t *new;
2138 	int entry;
2139 
2140 	entry = cp->rx_old[ring];
2141 
2142 	new = cas_page_swap(cp, ring, index);
2143 	cp->init_rxds[ring][entry].buffer = cpu_to_le64(new->dma_addr);
2144 	cp->init_rxds[ring][entry].index  =
2145 		cpu_to_le64(CAS_BASE(RX_INDEX_NUM, index) |
2146 			    CAS_BASE(RX_INDEX_RING, ring));
2147 
2148 	entry = RX_DESC_ENTRY(ring, entry + 1);
2149 	cp->rx_old[ring] = entry;
2150 
2151 	if (entry % 4)
2152 		return;
2153 
2154 	if (ring == 0)
2155 		writel(entry, cp->regs + REG_RX_KICK);
2156 	else if ((N_RX_DESC_RINGS > 1) &&
2157 		 (cp->cas_flags & CAS_FLAG_REG_PLUS))
2158 		writel(entry, cp->regs + REG_PLUS_RX_KICK1);
2159 }
2160 
2161 
2162 /* only when things are bad */
2163 static int cas_post_rxds_ringN(struct cas *cp, int ring, int num)
2164 {
2165 	unsigned int entry, last, count, released;
2166 	int cluster;
2167 	cas_page_t **page = cp->rx_pages[ring];
2168 
2169 	entry = cp->rx_old[ring];
2170 
2171 	netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2172 		     "rxd[%d] interrupt, done: %d\n", ring, entry);
2173 
2174 	cluster = -1;
2175 	count = entry & 0x3;
2176 	last = RX_DESC_ENTRY(ring, num ? entry + num - 4: entry - 4);
2177 	released = 0;
2178 	while (entry != last) {
2179 		/* make a new buffer if it's still in use */
2180 		if (page_count(page[entry]->buffer) > 1) {
2181 			cas_page_t *new = cas_page_dequeue(cp);
2182 			if (!new) {
2183 				/* let the timer know that we need to
2184 				 * do this again
2185 				 */
2186 				cp->cas_flags |= CAS_FLAG_RXD_POST(ring);
2187 				if (!timer_pending(&cp->link_timer))
2188 					mod_timer(&cp->link_timer, jiffies +
2189 						  CAS_LINK_FAST_TIMEOUT);
2190 				cp->rx_old[ring]  = entry;
2191 				cp->rx_last[ring] = num ? num - released : 0;
2192 				return -ENOMEM;
2193 			}
2194 			spin_lock(&cp->rx_inuse_lock);
2195 			list_add(&page[entry]->list, &cp->rx_inuse_list);
2196 			spin_unlock(&cp->rx_inuse_lock);
2197 			cp->init_rxds[ring][entry].buffer =
2198 				cpu_to_le64(new->dma_addr);
2199 			page[entry] = new;
2200 
2201 		}
2202 
2203 		if (++count == 4) {
2204 			cluster = entry;
2205 			count = 0;
2206 		}
2207 		released++;
2208 		entry = RX_DESC_ENTRY(ring, entry + 1);
2209 	}
2210 	cp->rx_old[ring] = entry;
2211 
2212 	if (cluster < 0)
2213 		return 0;
2214 
2215 	if (ring == 0)
2216 		writel(cluster, cp->regs + REG_RX_KICK);
2217 	else if ((N_RX_DESC_RINGS > 1) &&
2218 		 (cp->cas_flags & CAS_FLAG_REG_PLUS))
2219 		writel(cluster, cp->regs + REG_PLUS_RX_KICK1);
2220 	return 0;
2221 }
2222 
2223 
2224 /* process a completion ring. packets are set up in three basic ways:
2225  * small packets: should be copied header + data in single buffer.
2226  * large packets: header and data in a single buffer.
2227  * split packets: header in a separate buffer from data.
2228  *                data may be in multiple pages. data may be > 256
2229  *                bytes but in a single page.
2230  *
2231  * NOTE: RX page posting is done in this routine as well. while there's
2232  *       the capability of using multiple RX completion rings, it isn't
2233  *       really worthwhile due to the fact that the page posting will
2234  *       force serialization on the single descriptor ring.
2235  */
2236 static int cas_rx_ringN(struct cas *cp, int ring, int budget)
2237 {
2238 	struct cas_rx_comp *rxcs = cp->init_rxcs[ring];
2239 	int entry, drops;
2240 	int npackets = 0;
2241 
2242 	netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2243 		     "rx[%d] interrupt, done: %d/%d\n",
2244 		     ring,
2245 		     readl(cp->regs + REG_RX_COMP_HEAD), cp->rx_new[ring]);
2246 
2247 	entry = cp->rx_new[ring];
2248 	drops = 0;
2249 	while (1) {
2250 		struct cas_rx_comp *rxc = rxcs + entry;
2251 		struct sk_buff *skb;
2252 		int type, len;
2253 		u64 words[4];
2254 		int i, dring;
2255 
2256 		words[0] = le64_to_cpu(rxc->word1);
2257 		words[1] = le64_to_cpu(rxc->word2);
2258 		words[2] = le64_to_cpu(rxc->word3);
2259 		words[3] = le64_to_cpu(rxc->word4);
2260 
2261 		/* don't touch if still owned by hw */
2262 		type = CAS_VAL(RX_COMP1_TYPE, words[0]);
2263 		if (type == 0)
2264 			break;
2265 
2266 		/* hw hasn't cleared the zero bit yet */
2267 		if (words[3] & RX_COMP4_ZERO) {
2268 			break;
2269 		}
2270 
2271 		/* get info on the packet */
2272 		if (words[3] & (RX_COMP4_LEN_MISMATCH | RX_COMP4_BAD)) {
2273 			spin_lock(&cp->stat_lock[ring]);
2274 			cp->net_stats[ring].rx_errors++;
2275 			if (words[3] & RX_COMP4_LEN_MISMATCH)
2276 				cp->net_stats[ring].rx_length_errors++;
2277 			if (words[3] & RX_COMP4_BAD)
2278 				cp->net_stats[ring].rx_crc_errors++;
2279 			spin_unlock(&cp->stat_lock[ring]);
2280 
2281 			/* We'll just return it to Cassini. */
2282 		drop_it:
2283 			spin_lock(&cp->stat_lock[ring]);
2284 			++cp->net_stats[ring].rx_dropped;
2285 			spin_unlock(&cp->stat_lock[ring]);
2286 			goto next;
2287 		}
2288 
2289 		len = cas_rx_process_pkt(cp, rxc, entry, words, &skb);
2290 		if (len < 0) {
2291 			++drops;
2292 			goto drop_it;
2293 		}
2294 
2295 		/* see if it's a flow re-assembly or not. the driver
2296 		 * itself handles release back up.
2297 		 */
2298 		if (RX_DONT_BATCH || (type == 0x2)) {
2299 			/* non-reassm: these always get released */
2300 			cas_skb_release(skb);
2301 		} else {
2302 			cas_rx_flow_pkt(cp, words, skb);
2303 		}
2304 
2305 		spin_lock(&cp->stat_lock[ring]);
2306 		cp->net_stats[ring].rx_packets++;
2307 		cp->net_stats[ring].rx_bytes += len;
2308 		spin_unlock(&cp->stat_lock[ring]);
2309 
2310 	next:
2311 		npackets++;
2312 
2313 		/* should it be released? */
2314 		if (words[0] & RX_COMP1_RELEASE_HDR) {
2315 			i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
2316 			dring = CAS_VAL(RX_INDEX_RING, i);
2317 			i = CAS_VAL(RX_INDEX_NUM, i);
2318 			cas_post_page(cp, dring, i);
2319 		}
2320 
2321 		if (words[0] & RX_COMP1_RELEASE_DATA) {
2322 			i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2323 			dring = CAS_VAL(RX_INDEX_RING, i);
2324 			i = CAS_VAL(RX_INDEX_NUM, i);
2325 			cas_post_page(cp, dring, i);
2326 		}
2327 
2328 		if (words[0] & RX_COMP1_RELEASE_NEXT) {
2329 			i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2330 			dring = CAS_VAL(RX_INDEX_RING, i);
2331 			i = CAS_VAL(RX_INDEX_NUM, i);
2332 			cas_post_page(cp, dring, i);
2333 		}
2334 
2335 		/* skip to the next entry */
2336 		entry = RX_COMP_ENTRY(ring, entry + 1 +
2337 				      CAS_VAL(RX_COMP1_SKIP, words[0]));
2338 #ifdef USE_NAPI
2339 		if (budget && (npackets >= budget))
2340 			break;
2341 #endif
2342 	}
2343 	cp->rx_new[ring] = entry;
2344 
2345 	if (drops)
2346 		netdev_info(cp->dev, "Memory squeeze, deferring packet\n");
2347 	return npackets;
2348 }
2349 
2350 
2351 /* put completion entries back on the ring */
2352 static void cas_post_rxcs_ringN(struct net_device *dev,
2353 				struct cas *cp, int ring)
2354 {
2355 	struct cas_rx_comp *rxc = cp->init_rxcs[ring];
2356 	int last, entry;
2357 
2358 	last = cp->rx_cur[ring];
2359 	entry = cp->rx_new[ring];
2360 	netif_printk(cp, intr, KERN_DEBUG, dev,
2361 		     "rxc[%d] interrupt, done: %d/%d\n",
2362 		     ring, readl(cp->regs + REG_RX_COMP_HEAD), entry);
2363 
2364 	/* zero and re-mark descriptors */
2365 	while (last != entry) {
2366 		cas_rxc_init(rxc + last);
2367 		last = RX_COMP_ENTRY(ring, last + 1);
2368 	}
2369 	cp->rx_cur[ring] = last;
2370 
2371 	if (ring == 0)
2372 		writel(last, cp->regs + REG_RX_COMP_TAIL);
2373 	else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
2374 		writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
2375 }
2376 
2377 
2378 
2379 /* cassini can use all four PCI interrupts for the completion ring.
2380  * rings 3 and 4 are identical
2381  */
2382 #if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
2383 static inline void cas_handle_irqN(struct net_device *dev,
2384 				   struct cas *cp, const u32 status,
2385 				   const int ring)
2386 {
2387 	if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
2388 		cas_post_rxcs_ringN(dev, cp, ring);
2389 }
2390 
2391 static irqreturn_t cas_interruptN(int irq, void *dev_id)
2392 {
2393 	struct net_device *dev = dev_id;
2394 	struct cas *cp = netdev_priv(dev);
2395 	unsigned long flags;
2396 	int ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
2397 	u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
2398 
2399 	/* check for shared irq */
2400 	if (status == 0)
2401 		return IRQ_NONE;
2402 
2403 	spin_lock_irqsave(&cp->lock, flags);
2404 	if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2405 #ifdef USE_NAPI
2406 		cas_mask_intr(cp);
2407 		napi_schedule(&cp->napi);
2408 #else
2409 		cas_rx_ringN(cp, ring, 0);
2410 #endif
2411 		status &= ~INTR_RX_DONE_ALT;
2412 	}
2413 
2414 	if (status)
2415 		cas_handle_irqN(dev, cp, status, ring);
2416 	spin_unlock_irqrestore(&cp->lock, flags);
2417 	return IRQ_HANDLED;
2418 }
2419 #endif
2420 
2421 #ifdef USE_PCI_INTB
2422 /* everything but rx packets */
2423 static inline void cas_handle_irq1(struct cas *cp, const u32 status)
2424 {
2425 	if (status & INTR_RX_BUF_UNAVAIL_1) {
2426 		/* Frame arrived, no free RX buffers available.
2427 		 * NOTE: we can get this on a link transition. */
2428 		cas_post_rxds_ringN(cp, 1, 0);
2429 		spin_lock(&cp->stat_lock[1]);
2430 		cp->net_stats[1].rx_dropped++;
2431 		spin_unlock(&cp->stat_lock[1]);
2432 	}
2433 
2434 	if (status & INTR_RX_BUF_AE_1)
2435 		cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
2436 				    RX_AE_FREEN_VAL(1));
2437 
2438 	if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2439 		cas_post_rxcs_ringN(cp, 1);
2440 }
2441 
2442 /* ring 2 handles a few more events than 3 and 4 */
2443 static irqreturn_t cas_interrupt1(int irq, void *dev_id)
2444 {
2445 	struct net_device *dev = dev_id;
2446 	struct cas *cp = netdev_priv(dev);
2447 	unsigned long flags;
2448 	u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2449 
2450 	/* check for shared interrupt */
2451 	if (status == 0)
2452 		return IRQ_NONE;
2453 
2454 	spin_lock_irqsave(&cp->lock, flags);
2455 	if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2456 #ifdef USE_NAPI
2457 		cas_mask_intr(cp);
2458 		napi_schedule(&cp->napi);
2459 #else
2460 		cas_rx_ringN(cp, 1, 0);
2461 #endif
2462 		status &= ~INTR_RX_DONE_ALT;
2463 	}
2464 	if (status)
2465 		cas_handle_irq1(cp, status);
2466 	spin_unlock_irqrestore(&cp->lock, flags);
2467 	return IRQ_HANDLED;
2468 }
2469 #endif
2470 
2471 static inline void cas_handle_irq(struct net_device *dev,
2472 				  struct cas *cp, const u32 status)
2473 {
2474 	/* housekeeping interrupts */
2475 	if (status & INTR_ERROR_MASK)
2476 		cas_abnormal_irq(dev, cp, status);
2477 
2478 	if (status & INTR_RX_BUF_UNAVAIL) {
2479 		/* Frame arrived, no free RX buffers available.
2480 		 * NOTE: we can get this on a link transition.
2481 		 */
2482 		cas_post_rxds_ringN(cp, 0, 0);
2483 		spin_lock(&cp->stat_lock[0]);
2484 		cp->net_stats[0].rx_dropped++;
2485 		spin_unlock(&cp->stat_lock[0]);
2486 	} else if (status & INTR_RX_BUF_AE) {
2487 		cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
2488 				    RX_AE_FREEN_VAL(0));
2489 	}
2490 
2491 	if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2492 		cas_post_rxcs_ringN(dev, cp, 0);
2493 }
2494 
2495 static irqreturn_t cas_interrupt(int irq, void *dev_id)
2496 {
2497 	struct net_device *dev = dev_id;
2498 	struct cas *cp = netdev_priv(dev);
2499 	unsigned long flags;
2500 	u32 status = readl(cp->regs + REG_INTR_STATUS);
2501 
2502 	if (status == 0)
2503 		return IRQ_NONE;
2504 
2505 	spin_lock_irqsave(&cp->lock, flags);
2506 	if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
2507 		cas_tx(dev, cp, status);
2508 		status &= ~(INTR_TX_ALL | INTR_TX_INTME);
2509 	}
2510 
2511 	if (status & INTR_RX_DONE) {
2512 #ifdef USE_NAPI
2513 		cas_mask_intr(cp);
2514 		napi_schedule(&cp->napi);
2515 #else
2516 		cas_rx_ringN(cp, 0, 0);
2517 #endif
2518 		status &= ~INTR_RX_DONE;
2519 	}
2520 
2521 	if (status)
2522 		cas_handle_irq(dev, cp, status);
2523 	spin_unlock_irqrestore(&cp->lock, flags);
2524 	return IRQ_HANDLED;
2525 }
2526 
2527 
2528 #ifdef USE_NAPI
2529 static int cas_poll(struct napi_struct *napi, int budget)
2530 {
2531 	struct cas *cp = container_of(napi, struct cas, napi);
2532 	struct net_device *dev = cp->dev;
2533 	int i, enable_intr, credits;
2534 	u32 status = readl(cp->regs + REG_INTR_STATUS);
2535 	unsigned long flags;
2536 
2537 	spin_lock_irqsave(&cp->lock, flags);
2538 	cas_tx(dev, cp, status);
2539 	spin_unlock_irqrestore(&cp->lock, flags);
2540 
2541 	/* NAPI rx packets. we spread the credits across all of the
2542 	 * rxc rings
2543 	 *
2544 	 * to make sure we're fair with the work we loop through each
2545 	 * ring N_RX_COMP_RING times with a request of
2546 	 * budget / N_RX_COMP_RINGS
2547 	 */
2548 	enable_intr = 1;
2549 	credits = 0;
2550 	for (i = 0; i < N_RX_COMP_RINGS; i++) {
2551 		int j;
2552 		for (j = 0; j < N_RX_COMP_RINGS; j++) {
2553 			credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
2554 			if (credits >= budget) {
2555 				enable_intr = 0;
2556 				goto rx_comp;
2557 			}
2558 		}
2559 	}
2560 
2561 rx_comp:
2562 	/* final rx completion */
2563 	spin_lock_irqsave(&cp->lock, flags);
2564 	if (status)
2565 		cas_handle_irq(dev, cp, status);
2566 
2567 #ifdef USE_PCI_INTB
2568 	if (N_RX_COMP_RINGS > 1) {
2569 		status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2570 		if (status)
2571 			cas_handle_irq1(dev, cp, status);
2572 	}
2573 #endif
2574 
2575 #ifdef USE_PCI_INTC
2576 	if (N_RX_COMP_RINGS > 2) {
2577 		status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
2578 		if (status)
2579 			cas_handle_irqN(dev, cp, status, 2);
2580 	}
2581 #endif
2582 
2583 #ifdef USE_PCI_INTD
2584 	if (N_RX_COMP_RINGS > 3) {
2585 		status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
2586 		if (status)
2587 			cas_handle_irqN(dev, cp, status, 3);
2588 	}
2589 #endif
2590 	spin_unlock_irqrestore(&cp->lock, flags);
2591 	if (enable_intr) {
2592 		napi_complete(napi);
2593 		cas_unmask_intr(cp);
2594 	}
2595 	return credits;
2596 }
2597 #endif
2598 
2599 #ifdef CONFIG_NET_POLL_CONTROLLER
2600 static void cas_netpoll(struct net_device *dev)
2601 {
2602 	struct cas *cp = netdev_priv(dev);
2603 
2604 	cas_disable_irq(cp, 0);
2605 	cas_interrupt(cp->pdev->irq, dev);
2606 	cas_enable_irq(cp, 0);
2607 
2608 #ifdef USE_PCI_INTB
2609 	if (N_RX_COMP_RINGS > 1) {
2610 		/* cas_interrupt1(); */
2611 	}
2612 #endif
2613 #ifdef USE_PCI_INTC
2614 	if (N_RX_COMP_RINGS > 2) {
2615 		/* cas_interruptN(); */
2616 	}
2617 #endif
2618 #ifdef USE_PCI_INTD
2619 	if (N_RX_COMP_RINGS > 3) {
2620 		/* cas_interruptN(); */
2621 	}
2622 #endif
2623 }
2624 #endif
2625 
2626 static void cas_tx_timeout(struct net_device *dev, unsigned int txqueue)
2627 {
2628 	struct cas *cp = netdev_priv(dev);
2629 
2630 	netdev_err(dev, "transmit timed out, resetting\n");
2631 	if (!cp->hw_running) {
2632 		netdev_err(dev, "hrm.. hw not running!\n");
2633 		return;
2634 	}
2635 
2636 	netdev_err(dev, "MIF_STATE[%08x]\n",
2637 		   readl(cp->regs + REG_MIF_STATE_MACHINE));
2638 
2639 	netdev_err(dev, "MAC_STATE[%08x]\n",
2640 		   readl(cp->regs + REG_MAC_STATE_MACHINE));
2641 
2642 	netdev_err(dev, "TX_STATE[%08x:%08x:%08x] FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
2643 		   readl(cp->regs + REG_TX_CFG),
2644 		   readl(cp->regs + REG_MAC_TX_STATUS),
2645 		   readl(cp->regs + REG_MAC_TX_CFG),
2646 		   readl(cp->regs + REG_TX_FIFO_PKT_CNT),
2647 		   readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
2648 		   readl(cp->regs + REG_TX_FIFO_READ_PTR),
2649 		   readl(cp->regs + REG_TX_SM_1),
2650 		   readl(cp->regs + REG_TX_SM_2));
2651 
2652 	netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
2653 		   readl(cp->regs + REG_RX_CFG),
2654 		   readl(cp->regs + REG_MAC_RX_STATUS),
2655 		   readl(cp->regs + REG_MAC_RX_CFG));
2656 
2657 	netdev_err(dev, "HP_STATE[%08x:%08x:%08x:%08x]\n",
2658 		   readl(cp->regs + REG_HP_STATE_MACHINE),
2659 		   readl(cp->regs + REG_HP_STATUS0),
2660 		   readl(cp->regs + REG_HP_STATUS1),
2661 		   readl(cp->regs + REG_HP_STATUS2));
2662 
2663 #if 1
2664 	atomic_inc(&cp->reset_task_pending);
2665 	atomic_inc(&cp->reset_task_pending_all);
2666 	schedule_work(&cp->reset_task);
2667 #else
2668 	atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
2669 	schedule_work(&cp->reset_task);
2670 #endif
2671 }
2672 
2673 static inline int cas_intme(int ring, int entry)
2674 {
2675 	/* Algorithm: IRQ every 1/2 of descriptors. */
2676 	if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
2677 		return 1;
2678 	return 0;
2679 }
2680 
2681 
2682 static void cas_write_txd(struct cas *cp, int ring, int entry,
2683 			  dma_addr_t mapping, int len, u64 ctrl, int last)
2684 {
2685 	struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
2686 
2687 	ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
2688 	if (cas_intme(ring, entry))
2689 		ctrl |= TX_DESC_INTME;
2690 	if (last)
2691 		ctrl |= TX_DESC_EOF;
2692 	txd->control = cpu_to_le64(ctrl);
2693 	txd->buffer = cpu_to_le64(mapping);
2694 }
2695 
2696 static inline void *tx_tiny_buf(struct cas *cp, const int ring,
2697 				const int entry)
2698 {
2699 	return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
2700 }
2701 
2702 static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
2703 				     const int entry, const int tentry)
2704 {
2705 	cp->tx_tiny_use[ring][tentry].nbufs++;
2706 	cp->tx_tiny_use[ring][entry].used = 1;
2707 	return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
2708 }
2709 
2710 static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
2711 				    struct sk_buff *skb)
2712 {
2713 	struct net_device *dev = cp->dev;
2714 	int entry, nr_frags, frag, tabort, tentry;
2715 	dma_addr_t mapping;
2716 	unsigned long flags;
2717 	u64 ctrl;
2718 	u32 len;
2719 
2720 	spin_lock_irqsave(&cp->tx_lock[ring], flags);
2721 
2722 	/* This is a hard error, log it. */
2723 	if (TX_BUFFS_AVAIL(cp, ring) <=
2724 	    CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
2725 		netif_stop_queue(dev);
2726 		spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
2727 		netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
2728 		return 1;
2729 	}
2730 
2731 	ctrl = 0;
2732 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2733 		const u64 csum_start_off = skb_checksum_start_offset(skb);
2734 		const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
2735 
2736 		ctrl =  TX_DESC_CSUM_EN |
2737 			CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
2738 			CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
2739 	}
2740 
2741 	entry = cp->tx_new[ring];
2742 	cp->tx_skbs[ring][entry] = skb;
2743 
2744 	nr_frags = skb_shinfo(skb)->nr_frags;
2745 	len = skb_headlen(skb);
2746 	mapping = dma_map_page(&cp->pdev->dev, virt_to_page(skb->data),
2747 			       offset_in_page(skb->data), len, DMA_TO_DEVICE);
2748 
2749 	tentry = entry;
2750 	tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
2751 	if (unlikely(tabort)) {
2752 		/* NOTE: len is always >  tabort */
2753 		cas_write_txd(cp, ring, entry, mapping, len - tabort,
2754 			      ctrl | TX_DESC_SOF, 0);
2755 		entry = TX_DESC_NEXT(ring, entry);
2756 
2757 		skb_copy_from_linear_data_offset(skb, len - tabort,
2758 			      tx_tiny_buf(cp, ring, entry), tabort);
2759 		mapping = tx_tiny_map(cp, ring, entry, tentry);
2760 		cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
2761 			      (nr_frags == 0));
2762 	} else {
2763 		cas_write_txd(cp, ring, entry, mapping, len, ctrl |
2764 			      TX_DESC_SOF, (nr_frags == 0));
2765 	}
2766 	entry = TX_DESC_NEXT(ring, entry);
2767 
2768 	for (frag = 0; frag < nr_frags; frag++) {
2769 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
2770 
2771 		len = skb_frag_size(fragp);
2772 		mapping = skb_frag_dma_map(&cp->pdev->dev, fragp, 0, len,
2773 					   DMA_TO_DEVICE);
2774 
2775 		tabort = cas_calc_tabort(cp, skb_frag_off(fragp), len);
2776 		if (unlikely(tabort)) {
2777 			/* NOTE: len is always > tabort */
2778 			cas_write_txd(cp, ring, entry, mapping, len - tabort,
2779 				      ctrl, 0);
2780 			entry = TX_DESC_NEXT(ring, entry);
2781 			memcpy_from_page(tx_tiny_buf(cp, ring, entry),
2782 					 skb_frag_page(fragp),
2783 					 skb_frag_off(fragp) + len - tabort,
2784 					 tabort);
2785 			mapping = tx_tiny_map(cp, ring, entry, tentry);
2786 			len     = tabort;
2787 		}
2788 
2789 		cas_write_txd(cp, ring, entry, mapping, len, ctrl,
2790 			      (frag + 1 == nr_frags));
2791 		entry = TX_DESC_NEXT(ring, entry);
2792 	}
2793 
2794 	cp->tx_new[ring] = entry;
2795 	if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
2796 		netif_stop_queue(dev);
2797 
2798 	netif_printk(cp, tx_queued, KERN_DEBUG, dev,
2799 		     "tx[%d] queued, slot %d, skblen %d, avail %d\n",
2800 		     ring, entry, skb->len, TX_BUFFS_AVAIL(cp, ring));
2801 	writel(entry, cp->regs + REG_TX_KICKN(ring));
2802 	spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
2803 	return 0;
2804 }
2805 
2806 static netdev_tx_t cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
2807 {
2808 	struct cas *cp = netdev_priv(dev);
2809 
2810 	/* this is only used as a load-balancing hint, so it doesn't
2811 	 * need to be SMP safe
2812 	 */
2813 	static int ring;
2814 
2815 	if (skb_padto(skb, cp->min_frame_size))
2816 		return NETDEV_TX_OK;
2817 
2818 	/* XXX: we need some higher-level QoS hooks to steer packets to
2819 	 *      individual queues.
2820 	 */
2821 	if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
2822 		return NETDEV_TX_BUSY;
2823 	return NETDEV_TX_OK;
2824 }
2825 
2826 static void cas_init_tx_dma(struct cas *cp)
2827 {
2828 	u64 desc_dma = cp->block_dvma;
2829 	unsigned long off;
2830 	u32 val;
2831 	int i;
2832 
2833 	/* set up tx completion writeback registers. must be 8-byte aligned */
2834 #ifdef USE_TX_COMPWB
2835 	off = offsetof(struct cas_init_block, tx_compwb);
2836 	writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
2837 	writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
2838 #endif
2839 
2840 	/* enable completion writebacks, enable paced mode,
2841 	 * disable read pipe, and disable pre-interrupt compwbs
2842 	 */
2843 	val =   TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
2844 		TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
2845 		TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
2846 		TX_CFG_INTR_COMPWB_DIS;
2847 
2848 	/* write out tx ring info and tx desc bases */
2849 	for (i = 0; i < MAX_TX_RINGS; i++) {
2850 		off = (unsigned long) cp->init_txds[i] -
2851 			(unsigned long) cp->init_block;
2852 
2853 		val |= CAS_TX_RINGN_BASE(i);
2854 		writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
2855 		writel((desc_dma + off) & 0xffffffff, cp->regs +
2856 		       REG_TX_DBN_LOW(i));
2857 		/* don't zero out the kick register here as the system
2858 		 * will wedge
2859 		 */
2860 	}
2861 	writel(val, cp->regs + REG_TX_CFG);
2862 
2863 	/* program max burst sizes. these numbers should be different
2864 	 * if doing QoS.
2865 	 */
2866 #ifdef USE_QOS
2867 	writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2868 	writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
2869 	writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
2870 	writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
2871 #else
2872 	writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2873 	writel(0x800, cp->regs + REG_TX_MAXBURST_1);
2874 	writel(0x800, cp->regs + REG_TX_MAXBURST_2);
2875 	writel(0x800, cp->regs + REG_TX_MAXBURST_3);
2876 #endif
2877 }
2878 
2879 /* Must be invoked under cp->lock. */
2880 static inline void cas_init_dma(struct cas *cp)
2881 {
2882 	cas_init_tx_dma(cp);
2883 	cas_init_rx_dma(cp);
2884 }
2885 
2886 static void cas_process_mc_list(struct cas *cp)
2887 {
2888 	u16 hash_table[16];
2889 	u32 crc;
2890 	struct netdev_hw_addr *ha;
2891 	int i = 1;
2892 
2893 	memset(hash_table, 0, sizeof(hash_table));
2894 	netdev_for_each_mc_addr(ha, cp->dev) {
2895 		if (i <= CAS_MC_EXACT_MATCH_SIZE) {
2896 			/* use the alternate mac address registers for the
2897 			 * first 15 multicast addresses
2898 			 */
2899 			writel((ha->addr[4] << 8) | ha->addr[5],
2900 			       cp->regs + REG_MAC_ADDRN(i*3 + 0));
2901 			writel((ha->addr[2] << 8) | ha->addr[3],
2902 			       cp->regs + REG_MAC_ADDRN(i*3 + 1));
2903 			writel((ha->addr[0] << 8) | ha->addr[1],
2904 			       cp->regs + REG_MAC_ADDRN(i*3 + 2));
2905 			i++;
2906 		}
2907 		else {
2908 			/* use hw hash table for the next series of
2909 			 * multicast addresses
2910 			 */
2911 			crc = ether_crc_le(ETH_ALEN, ha->addr);
2912 			crc >>= 24;
2913 			hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
2914 		}
2915 	}
2916 	for (i = 0; i < 16; i++)
2917 		writel(hash_table[i], cp->regs + REG_MAC_HASH_TABLEN(i));
2918 }
2919 
2920 /* Must be invoked under cp->lock. */
2921 static u32 cas_setup_multicast(struct cas *cp)
2922 {
2923 	u32 rxcfg = 0;
2924 	int i;
2925 
2926 	if (cp->dev->flags & IFF_PROMISC) {
2927 		rxcfg |= MAC_RX_CFG_PROMISC_EN;
2928 
2929 	} else if (cp->dev->flags & IFF_ALLMULTI) {
2930 	    	for (i=0; i < 16; i++)
2931 			writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
2932 		rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2933 
2934 	} else {
2935 		cas_process_mc_list(cp);
2936 		rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2937 	}
2938 
2939 	return rxcfg;
2940 }
2941 
2942 /* must be invoked under cp->stat_lock[N_TX_RINGS] */
2943 static void cas_clear_mac_err(struct cas *cp)
2944 {
2945 	writel(0, cp->regs + REG_MAC_COLL_NORMAL);
2946 	writel(0, cp->regs + REG_MAC_COLL_FIRST);
2947 	writel(0, cp->regs + REG_MAC_COLL_EXCESS);
2948 	writel(0, cp->regs + REG_MAC_COLL_LATE);
2949 	writel(0, cp->regs + REG_MAC_TIMER_DEFER);
2950 	writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
2951 	writel(0, cp->regs + REG_MAC_RECV_FRAME);
2952 	writel(0, cp->regs + REG_MAC_LEN_ERR);
2953 	writel(0, cp->regs + REG_MAC_ALIGN_ERR);
2954 	writel(0, cp->regs + REG_MAC_FCS_ERR);
2955 	writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
2956 }
2957 
2958 
2959 static void cas_mac_reset(struct cas *cp)
2960 {
2961 	int i;
2962 
2963 	/* do both TX and RX reset */
2964 	writel(0x1, cp->regs + REG_MAC_TX_RESET);
2965 	writel(0x1, cp->regs + REG_MAC_RX_RESET);
2966 
2967 	/* wait for TX */
2968 	i = STOP_TRIES;
2969 	while (i-- > 0) {
2970 		if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
2971 			break;
2972 		udelay(10);
2973 	}
2974 
2975 	/* wait for RX */
2976 	i = STOP_TRIES;
2977 	while (i-- > 0) {
2978 		if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
2979 			break;
2980 		udelay(10);
2981 	}
2982 
2983 	if (readl(cp->regs + REG_MAC_TX_RESET) |
2984 	    readl(cp->regs + REG_MAC_RX_RESET))
2985 		netdev_err(cp->dev, "mac tx[%d]/rx[%d] reset failed [%08x]\n",
2986 			   readl(cp->regs + REG_MAC_TX_RESET),
2987 			   readl(cp->regs + REG_MAC_RX_RESET),
2988 			   readl(cp->regs + REG_MAC_STATE_MACHINE));
2989 }
2990 
2991 
2992 /* Must be invoked under cp->lock. */
2993 static void cas_init_mac(struct cas *cp)
2994 {
2995 	const unsigned char *e = &cp->dev->dev_addr[0];
2996 	int i;
2997 	cas_mac_reset(cp);
2998 
2999 	/* setup core arbitration weight register */
3000 	writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
3001 
3002 #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
3003 	/* set the infinite burst register for chips that don't have
3004 	 * pci issues.
3005 	 */
3006 	if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
3007 		writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
3008 #endif
3009 
3010 	writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
3011 
3012 	writel(0x00, cp->regs + REG_MAC_IPG0);
3013 	writel(0x08, cp->regs + REG_MAC_IPG1);
3014 	writel(0x04, cp->regs + REG_MAC_IPG2);
3015 
3016 	/* change later for 802.3z */
3017 	writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
3018 
3019 	/* min frame + FCS */
3020 	writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
3021 
3022 	/* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
3023 	 * specify the maximum frame size to prevent RX tag errors on
3024 	 * oversized frames.
3025 	 */
3026 	writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
3027 	       CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
3028 			(CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
3029 	       cp->regs + REG_MAC_FRAMESIZE_MAX);
3030 
3031 	/* NOTE: crc_size is used as a surrogate for half-duplex.
3032 	 * workaround saturn half-duplex issue by increasing preamble
3033 	 * size to 65 bytes.
3034 	 */
3035 	if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
3036 		writel(0x41, cp->regs + REG_MAC_PA_SIZE);
3037 	else
3038 		writel(0x07, cp->regs + REG_MAC_PA_SIZE);
3039 	writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
3040 	writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
3041 	writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
3042 
3043 	writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
3044 
3045 	writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
3046 	writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
3047 	writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
3048 	writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
3049 	writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
3050 
3051 	/* setup mac address in perfect filter array */
3052 	for (i = 0; i < 45; i++)
3053 		writel(0x0, cp->regs + REG_MAC_ADDRN(i));
3054 
3055 	writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
3056 	writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
3057 	writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
3058 
3059 	writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
3060 	writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
3061 	writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
3062 
3063 	cp->mac_rx_cfg = cas_setup_multicast(cp);
3064 
3065 	spin_lock(&cp->stat_lock[N_TX_RINGS]);
3066 	cas_clear_mac_err(cp);
3067 	spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3068 
3069 	/* Setup MAC interrupts.  We want to get all of the interesting
3070 	 * counter expiration events, but we do not want to hear about
3071 	 * normal rx/tx as the DMA engine tells us that.
3072 	 */
3073 	writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
3074 	writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
3075 
3076 	/* Don't enable even the PAUSE interrupts for now, we
3077 	 * make no use of those events other than to record them.
3078 	 */
3079 	writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
3080 }
3081 
3082 /* Must be invoked under cp->lock. */
3083 static void cas_init_pause_thresholds(struct cas *cp)
3084 {
3085 	/* Calculate pause thresholds.  Setting the OFF threshold to the
3086 	 * full RX fifo size effectively disables PAUSE generation
3087 	 */
3088 	if (cp->rx_fifo_size <= (2 * 1024)) {
3089 		cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
3090 	} else {
3091 		int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
3092 		if (max_frame * 3 > cp->rx_fifo_size) {
3093 			cp->rx_pause_off = 7104;
3094 			cp->rx_pause_on  = 960;
3095 		} else {
3096 			int off = (cp->rx_fifo_size - (max_frame * 2));
3097 			int on = off - max_frame;
3098 			cp->rx_pause_off = off;
3099 			cp->rx_pause_on = on;
3100 		}
3101 	}
3102 }
3103 
3104 static int cas_vpd_match(const void __iomem *p, const char *str)
3105 {
3106 	int len = strlen(str) + 1;
3107 	int i;
3108 
3109 	for (i = 0; i < len; i++) {
3110 		if (readb(p + i) != str[i])
3111 			return 0;
3112 	}
3113 	return 1;
3114 }
3115 
3116 
3117 /* get the mac address by reading the vpd information in the rom.
3118  * also get the phy type and determine if there's an entropy generator.
3119  * NOTE: this is a bit convoluted for the following reasons:
3120  *  1) vpd info has order-dependent mac addresses for multinic cards
3121  *  2) the only way to determine the nic order is to use the slot
3122  *     number.
3123  *  3) fiber cards don't have bridges, so their slot numbers don't
3124  *     mean anything.
3125  *  4) we don't actually know we have a fiber card until after
3126  *     the mac addresses are parsed.
3127  */
3128 static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
3129 			    const int offset)
3130 {
3131 	void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
3132 	void __iomem *base, *kstart;
3133 	int i, len;
3134 	int found = 0;
3135 #define VPD_FOUND_MAC        0x01
3136 #define VPD_FOUND_PHY        0x02
3137 
3138 	int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
3139 	int mac_off  = 0;
3140 
3141 #if defined(CONFIG_SPARC)
3142 	const unsigned char *addr;
3143 #endif
3144 
3145 	/* give us access to the PROM */
3146 	writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
3147 	       cp->regs + REG_BIM_LOCAL_DEV_EN);
3148 
3149 	/* check for an expansion rom */
3150 	if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
3151 		goto use_random_mac_addr;
3152 
3153 	/* search for beginning of vpd */
3154 	base = NULL;
3155 	for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
3156 		/* check for PCIR */
3157 		if ((readb(p + i + 0) == 0x50) &&
3158 		    (readb(p + i + 1) == 0x43) &&
3159 		    (readb(p + i + 2) == 0x49) &&
3160 		    (readb(p + i + 3) == 0x52)) {
3161 			base = p + (readb(p + i + 8) |
3162 				    (readb(p + i + 9) << 8));
3163 			break;
3164 		}
3165 	}
3166 
3167 	if (!base || (readb(base) != 0x82))
3168 		goto use_random_mac_addr;
3169 
3170 	i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
3171 	while (i < EXPANSION_ROM_SIZE) {
3172 		if (readb(base + i) != 0x90) /* no vpd found */
3173 			goto use_random_mac_addr;
3174 
3175 		/* found a vpd field */
3176 		len = readb(base + i + 1) | (readb(base + i + 2) << 8);
3177 
3178 		/* extract keywords */
3179 		kstart = base + i + 3;
3180 		p = kstart;
3181 		while ((p - kstart) < len) {
3182 			int klen = readb(p + 2);
3183 			int j;
3184 			char type;
3185 
3186 			p += 3;
3187 
3188 			/* look for the following things:
3189 			 * -- correct length == 29
3190 			 * 3 (type) + 2 (size) +
3191 			 * 18 (strlen("local-mac-address") + 1) +
3192 			 * 6 (mac addr)
3193 			 * -- VPD Instance 'I'
3194 			 * -- VPD Type Bytes 'B'
3195 			 * -- VPD data length == 6
3196 			 * -- property string == local-mac-address
3197 			 *
3198 			 * -- correct length == 24
3199 			 * 3 (type) + 2 (size) +
3200 			 * 12 (strlen("entropy-dev") + 1) +
3201 			 * 7 (strlen("vms110") + 1)
3202 			 * -- VPD Instance 'I'
3203 			 * -- VPD Type String 'B'
3204 			 * -- VPD data length == 7
3205 			 * -- property string == entropy-dev
3206 			 *
3207 			 * -- correct length == 18
3208 			 * 3 (type) + 2 (size) +
3209 			 * 9 (strlen("phy-type") + 1) +
3210 			 * 4 (strlen("pcs") + 1)
3211 			 * -- VPD Instance 'I'
3212 			 * -- VPD Type String 'S'
3213 			 * -- VPD data length == 4
3214 			 * -- property string == phy-type
3215 			 *
3216 			 * -- correct length == 23
3217 			 * 3 (type) + 2 (size) +
3218 			 * 14 (strlen("phy-interface") + 1) +
3219 			 * 4 (strlen("pcs") + 1)
3220 			 * -- VPD Instance 'I'
3221 			 * -- VPD Type String 'S'
3222 			 * -- VPD data length == 4
3223 			 * -- property string == phy-interface
3224 			 */
3225 			if (readb(p) != 'I')
3226 				goto next;
3227 
3228 			/* finally, check string and length */
3229 			type = readb(p + 3);
3230 			if (type == 'B') {
3231 				if ((klen == 29) && readb(p + 4) == 6 &&
3232 				    cas_vpd_match(p + 5,
3233 						  "local-mac-address")) {
3234 					if (mac_off++ > offset)
3235 						goto next;
3236 
3237 					/* set mac address */
3238 					for (j = 0; j < 6; j++)
3239 						dev_addr[j] =
3240 							readb(p + 23 + j);
3241 					goto found_mac;
3242 				}
3243 			}
3244 
3245 			if (type != 'S')
3246 				goto next;
3247 
3248 #ifdef USE_ENTROPY_DEV
3249 			if ((klen == 24) &&
3250 			    cas_vpd_match(p + 5, "entropy-dev") &&
3251 			    cas_vpd_match(p + 17, "vms110")) {
3252 				cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
3253 				goto next;
3254 			}
3255 #endif
3256 
3257 			if (found & VPD_FOUND_PHY)
3258 				goto next;
3259 
3260 			if ((klen == 18) && readb(p + 4) == 4 &&
3261 			    cas_vpd_match(p + 5, "phy-type")) {
3262 				if (cas_vpd_match(p + 14, "pcs")) {
3263 					phy_type = CAS_PHY_SERDES;
3264 					goto found_phy;
3265 				}
3266 			}
3267 
3268 			if ((klen == 23) && readb(p + 4) == 4 &&
3269 			    cas_vpd_match(p + 5, "phy-interface")) {
3270 				if (cas_vpd_match(p + 19, "pcs")) {
3271 					phy_type = CAS_PHY_SERDES;
3272 					goto found_phy;
3273 				}
3274 			}
3275 found_mac:
3276 			found |= VPD_FOUND_MAC;
3277 			goto next;
3278 
3279 found_phy:
3280 			found |= VPD_FOUND_PHY;
3281 
3282 next:
3283 			p += klen;
3284 		}
3285 		i += len + 3;
3286 	}
3287 
3288 use_random_mac_addr:
3289 	if (found & VPD_FOUND_MAC)
3290 		goto done;
3291 
3292 #if defined(CONFIG_SPARC)
3293 	addr = of_get_property(cp->of_node, "local-mac-address", NULL);
3294 	if (addr != NULL) {
3295 		memcpy(dev_addr, addr, ETH_ALEN);
3296 		goto done;
3297 	}
3298 #endif
3299 
3300 	/* Sun MAC prefix then 3 random bytes. */
3301 	pr_info("MAC address not found in ROM VPD\n");
3302 	dev_addr[0] = 0x08;
3303 	dev_addr[1] = 0x00;
3304 	dev_addr[2] = 0x20;
3305 	get_random_bytes(dev_addr + 3, 3);
3306 
3307 done:
3308 	writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
3309 	return phy_type;
3310 }
3311 
3312 /* check pci invariants */
3313 static void cas_check_pci_invariants(struct cas *cp)
3314 {
3315 	struct pci_dev *pdev = cp->pdev;
3316 
3317 	cp->cas_flags = 0;
3318 	if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
3319 	    (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
3320 		if (pdev->revision >= CAS_ID_REVPLUS)
3321 			cp->cas_flags |= CAS_FLAG_REG_PLUS;
3322 		if (pdev->revision < CAS_ID_REVPLUS02u)
3323 			cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
3324 
3325 		/* Original Cassini supports HW CSUM, but it's not
3326 		 * enabled by default as it can trigger TX hangs.
3327 		 */
3328 		if (pdev->revision < CAS_ID_REV2)
3329 			cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
3330 	} else {
3331 		/* Only sun has original cassini chips.  */
3332 		cp->cas_flags |= CAS_FLAG_REG_PLUS;
3333 
3334 		/* We use a flag because the same phy might be externally
3335 		 * connected.
3336 		 */
3337 		if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
3338 		    (pdev->device == PCI_DEVICE_ID_NS_SATURN))
3339 			cp->cas_flags |= CAS_FLAG_SATURN;
3340 	}
3341 }
3342 
3343 
3344 static int cas_check_invariants(struct cas *cp)
3345 {
3346 	struct pci_dev *pdev = cp->pdev;
3347 	u8 addr[ETH_ALEN];
3348 	u32 cfg;
3349 	int i;
3350 
3351 	/* get page size for rx buffers. */
3352 	cp->page_order = 0;
3353 #ifdef USE_PAGE_ORDER
3354 	if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
3355 		/* see if we can allocate larger pages */
3356 		struct page *page = alloc_pages(GFP_ATOMIC,
3357 						CAS_JUMBO_PAGE_SHIFT -
3358 						PAGE_SHIFT);
3359 		if (page) {
3360 			__free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
3361 			cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
3362 		} else {
3363 			printk("MTU limited to %d bytes\n", CAS_MAX_MTU);
3364 		}
3365 	}
3366 #endif
3367 	cp->page_size = (PAGE_SIZE << cp->page_order);
3368 
3369 	/* Fetch the FIFO configurations. */
3370 	cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
3371 	cp->rx_fifo_size = RX_FIFO_SIZE;
3372 
3373 	/* finish phy determination. MDIO1 takes precedence over MDIO0 if
3374 	 * they're both connected.
3375 	 */
3376 	cp->phy_type = cas_get_vpd_info(cp, addr, PCI_SLOT(pdev->devfn));
3377 	eth_hw_addr_set(cp->dev, addr);
3378 	if (cp->phy_type & CAS_PHY_SERDES) {
3379 		cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3380 		return 0; /* no more checking needed */
3381 	}
3382 
3383 	/* MII */
3384 	cfg = readl(cp->regs + REG_MIF_CFG);
3385 	if (cfg & MIF_CFG_MDIO_1) {
3386 		cp->phy_type = CAS_PHY_MII_MDIO1;
3387 	} else if (cfg & MIF_CFG_MDIO_0) {
3388 		cp->phy_type = CAS_PHY_MII_MDIO0;
3389 	}
3390 
3391 	cas_mif_poll(cp, 0);
3392 	writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3393 
3394 	for (i = 0; i < 32; i++) {
3395 		u32 phy_id;
3396 		int j;
3397 
3398 		for (j = 0; j < 3; j++) {
3399 			cp->phy_addr = i;
3400 			phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
3401 			phy_id |= cas_phy_read(cp, MII_PHYSID2);
3402 			if (phy_id && (phy_id != 0xFFFFFFFF)) {
3403 				cp->phy_id = phy_id;
3404 				goto done;
3405 			}
3406 		}
3407 	}
3408 	pr_err("MII phy did not respond [%08x]\n",
3409 	       readl(cp->regs + REG_MIF_STATE_MACHINE));
3410 	return -1;
3411 
3412 done:
3413 	/* see if we can do gigabit */
3414 	cfg = cas_phy_read(cp, MII_BMSR);
3415 	if ((cfg & CAS_BMSR_1000_EXTEND) &&
3416 	    cas_phy_read(cp, CAS_MII_1000_EXTEND))
3417 		cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3418 	return 0;
3419 }
3420 
3421 /* Must be invoked under cp->lock. */
3422 static inline void cas_start_dma(struct cas *cp)
3423 {
3424 	int i;
3425 	u32 val;
3426 	int txfailed = 0;
3427 
3428 	/* enable dma */
3429 	val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
3430 	writel(val, cp->regs + REG_TX_CFG);
3431 	val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
3432 	writel(val, cp->regs + REG_RX_CFG);
3433 
3434 	/* enable the mac */
3435 	val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
3436 	writel(val, cp->regs + REG_MAC_TX_CFG);
3437 	val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
3438 	writel(val, cp->regs + REG_MAC_RX_CFG);
3439 
3440 	i = STOP_TRIES;
3441 	while (i-- > 0) {
3442 		val = readl(cp->regs + REG_MAC_TX_CFG);
3443 		if ((val & MAC_TX_CFG_EN))
3444 			break;
3445 		udelay(10);
3446 	}
3447 	if (i < 0) txfailed = 1;
3448 	i = STOP_TRIES;
3449 	while (i-- > 0) {
3450 		val = readl(cp->regs + REG_MAC_RX_CFG);
3451 		if ((val & MAC_RX_CFG_EN)) {
3452 			if (txfailed) {
3453 				netdev_err(cp->dev,
3454 					   "enabling mac failed [tx:%08x:%08x]\n",
3455 					   readl(cp->regs + REG_MIF_STATE_MACHINE),
3456 					   readl(cp->regs + REG_MAC_STATE_MACHINE));
3457 			}
3458 			goto enable_rx_done;
3459 		}
3460 		udelay(10);
3461 	}
3462 	netdev_err(cp->dev, "enabling mac failed [%s:%08x:%08x]\n",
3463 		   (txfailed ? "tx,rx" : "rx"),
3464 		   readl(cp->regs + REG_MIF_STATE_MACHINE),
3465 		   readl(cp->regs + REG_MAC_STATE_MACHINE));
3466 
3467 enable_rx_done:
3468 	cas_unmask_intr(cp); /* enable interrupts */
3469 	writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
3470 	writel(0, cp->regs + REG_RX_COMP_TAIL);
3471 
3472 	if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
3473 		if (N_RX_DESC_RINGS > 1)
3474 			writel(RX_DESC_RINGN_SIZE(1) - 4,
3475 			       cp->regs + REG_PLUS_RX_KICK1);
3476 	}
3477 }
3478 
3479 /* Must be invoked under cp->lock. */
3480 static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
3481 				   int *pause)
3482 {
3483 	u32 val = readl(cp->regs + REG_PCS_MII_LPA);
3484 	*fd     = (val & PCS_MII_LPA_FD) ? 1 : 0;
3485 	*pause  = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
3486 	if (val & PCS_MII_LPA_ASYM_PAUSE)
3487 		*pause |= 0x10;
3488 	*spd = 1000;
3489 }
3490 
3491 /* Must be invoked under cp->lock. */
3492 static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
3493 				   int *pause)
3494 {
3495 	u32 val;
3496 
3497 	*fd = 0;
3498 	*spd = 10;
3499 	*pause = 0;
3500 
3501 	/* use GMII registers */
3502 	val = cas_phy_read(cp, MII_LPA);
3503 	if (val & CAS_LPA_PAUSE)
3504 		*pause = 0x01;
3505 
3506 	if (val & CAS_LPA_ASYM_PAUSE)
3507 		*pause |= 0x10;
3508 
3509 	if (val & LPA_DUPLEX)
3510 		*fd = 1;
3511 	if (val & LPA_100)
3512 		*spd = 100;
3513 
3514 	if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
3515 		val = cas_phy_read(cp, CAS_MII_1000_STATUS);
3516 		if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
3517 			*spd = 1000;
3518 		if (val & CAS_LPA_1000FULL)
3519 			*fd = 1;
3520 	}
3521 }
3522 
3523 /* A link-up condition has occurred, initialize and enable the
3524  * rest of the chip.
3525  *
3526  * Must be invoked under cp->lock.
3527  */
3528 static void cas_set_link_modes(struct cas *cp)
3529 {
3530 	u32 val;
3531 	int full_duplex, speed, pause;
3532 
3533 	full_duplex = 0;
3534 	speed = 10;
3535 	pause = 0;
3536 
3537 	if (CAS_PHY_MII(cp->phy_type)) {
3538 		cas_mif_poll(cp, 0);
3539 		val = cas_phy_read(cp, MII_BMCR);
3540 		if (val & BMCR_ANENABLE) {
3541 			cas_read_mii_link_mode(cp, &full_duplex, &speed,
3542 					       &pause);
3543 		} else {
3544 			if (val & BMCR_FULLDPLX)
3545 				full_duplex = 1;
3546 
3547 			if (val & BMCR_SPEED100)
3548 				speed = 100;
3549 			else if (val & CAS_BMCR_SPEED1000)
3550 				speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
3551 					1000 : 100;
3552 		}
3553 		cas_mif_poll(cp, 1);
3554 
3555 	} else {
3556 		val = readl(cp->regs + REG_PCS_MII_CTRL);
3557 		cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
3558 		if ((val & PCS_MII_AUTONEG_EN) == 0) {
3559 			if (val & PCS_MII_CTRL_DUPLEX)
3560 				full_duplex = 1;
3561 		}
3562 	}
3563 
3564 	netif_info(cp, link, cp->dev, "Link up at %d Mbps, %s-duplex\n",
3565 		   speed, full_duplex ? "full" : "half");
3566 
3567 	val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
3568 	if (CAS_PHY_MII(cp->phy_type)) {
3569 		val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
3570 		if (!full_duplex)
3571 			val |= MAC_XIF_DISABLE_ECHO;
3572 	}
3573 	if (full_duplex)
3574 		val |= MAC_XIF_FDPLX_LED;
3575 	if (speed == 1000)
3576 		val |= MAC_XIF_GMII_MODE;
3577 	writel(val, cp->regs + REG_MAC_XIF_CFG);
3578 
3579 	/* deal with carrier and collision detect. */
3580 	val = MAC_TX_CFG_IPG_EN;
3581 	if (full_duplex) {
3582 		val |= MAC_TX_CFG_IGNORE_CARRIER;
3583 		val |= MAC_TX_CFG_IGNORE_COLL;
3584 	} else {
3585 #ifndef USE_CSMA_CD_PROTO
3586 		val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
3587 		val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
3588 #endif
3589 	}
3590 	/* val now set up for REG_MAC_TX_CFG */
3591 
3592 	/* If gigabit and half-duplex, enable carrier extension
3593 	 * mode.  increase slot time to 512 bytes as well.
3594 	 * else, disable it and make sure slot time is 64 bytes.
3595 	 * also activate checksum bug workaround
3596 	 */
3597 	if ((speed == 1000) && !full_duplex) {
3598 		writel(val | MAC_TX_CFG_CARRIER_EXTEND,
3599 		       cp->regs + REG_MAC_TX_CFG);
3600 
3601 		val = readl(cp->regs + REG_MAC_RX_CFG);
3602 		val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
3603 		writel(val | MAC_RX_CFG_CARRIER_EXTEND,
3604 		       cp->regs + REG_MAC_RX_CFG);
3605 
3606 		writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
3607 
3608 		cp->crc_size = 4;
3609 		/* minimum size gigabit frame at half duplex */
3610 		cp->min_frame_size = CAS_1000MB_MIN_FRAME;
3611 
3612 	} else {
3613 		writel(val, cp->regs + REG_MAC_TX_CFG);
3614 
3615 		/* checksum bug workaround. don't strip FCS when in
3616 		 * half-duplex mode
3617 		 */
3618 		val = readl(cp->regs + REG_MAC_RX_CFG);
3619 		if (full_duplex) {
3620 			val |= MAC_RX_CFG_STRIP_FCS;
3621 			cp->crc_size = 0;
3622 			cp->min_frame_size = CAS_MIN_MTU;
3623 		} else {
3624 			val &= ~MAC_RX_CFG_STRIP_FCS;
3625 			cp->crc_size = 4;
3626 			cp->min_frame_size = CAS_MIN_FRAME;
3627 		}
3628 		writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
3629 		       cp->regs + REG_MAC_RX_CFG);
3630 		writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
3631 	}
3632 
3633 	if (netif_msg_link(cp)) {
3634 		if (pause & 0x01) {
3635 			netdev_info(cp->dev, "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
3636 				    cp->rx_fifo_size,
3637 				    cp->rx_pause_off,
3638 				    cp->rx_pause_on);
3639 		} else if (pause & 0x10) {
3640 			netdev_info(cp->dev, "TX pause enabled\n");
3641 		} else {
3642 			netdev_info(cp->dev, "Pause is disabled\n");
3643 		}
3644 	}
3645 
3646 	val = readl(cp->regs + REG_MAC_CTRL_CFG);
3647 	val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
3648 	if (pause) { /* symmetric or asymmetric pause */
3649 		val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
3650 		if (pause & 0x01) { /* symmetric pause */
3651 			val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
3652 		}
3653 	}
3654 	writel(val, cp->regs + REG_MAC_CTRL_CFG);
3655 	cas_start_dma(cp);
3656 }
3657 
3658 /* Must be invoked under cp->lock. */
3659 static void cas_init_hw(struct cas *cp, int restart_link)
3660 {
3661 	if (restart_link)
3662 		cas_phy_init(cp);
3663 
3664 	cas_init_pause_thresholds(cp);
3665 	cas_init_mac(cp);
3666 	cas_init_dma(cp);
3667 
3668 	if (restart_link) {
3669 		/* Default aneg parameters */
3670 		cp->timer_ticks = 0;
3671 		cas_begin_auto_negotiation(cp, NULL);
3672 	} else if (cp->lstate == link_up) {
3673 		cas_set_link_modes(cp);
3674 		netif_carrier_on(cp->dev);
3675 	}
3676 }
3677 
3678 /* Must be invoked under cp->lock. on earlier cassini boards,
3679  * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
3680  * let it settle out, and then restore pci state.
3681  */
3682 static void cas_hard_reset(struct cas *cp)
3683 {
3684 	writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
3685 	udelay(20);
3686 	pci_restore_state(cp->pdev);
3687 }
3688 
3689 
3690 static void cas_global_reset(struct cas *cp, int blkflag)
3691 {
3692 	int limit;
3693 
3694 	/* issue a global reset. don't use RSTOUT. */
3695 	if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
3696 		/* For PCS, when the blkflag is set, we should set the
3697 		 * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
3698 		 * the last autonegotiation from being cleared.  We'll
3699 		 * need some special handling if the chip is set into a
3700 		 * loopback mode.
3701 		 */
3702 		writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
3703 		       cp->regs + REG_SW_RESET);
3704 	} else {
3705 		writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
3706 	}
3707 
3708 	/* need to wait at least 3ms before polling register */
3709 	mdelay(3);
3710 
3711 	limit = STOP_TRIES;
3712 	while (limit-- > 0) {
3713 		u32 val = readl(cp->regs + REG_SW_RESET);
3714 		if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
3715 			goto done;
3716 		udelay(10);
3717 	}
3718 	netdev_err(cp->dev, "sw reset failed\n");
3719 
3720 done:
3721 	/* enable various BIM interrupts */
3722 	writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
3723 	       BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
3724 
3725 	/* clear out pci error status mask for handled errors.
3726 	 * we don't deal with DMA counter overflows as they happen
3727 	 * all the time.
3728 	 */
3729 	writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
3730 			       PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
3731 			       PCI_ERR_BIM_DMA_READ), cp->regs +
3732 	       REG_PCI_ERR_STATUS_MASK);
3733 
3734 	/* set up for MII by default to address mac rx reset timeout
3735 	 * issue
3736 	 */
3737 	writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3738 }
3739 
3740 static void cas_reset(struct cas *cp, int blkflag)
3741 {
3742 	u32 val;
3743 
3744 	cas_mask_intr(cp);
3745 	cas_global_reset(cp, blkflag);
3746 	cas_mac_reset(cp);
3747 	cas_entropy_reset(cp);
3748 
3749 	/* disable dma engines. */
3750 	val = readl(cp->regs + REG_TX_CFG);
3751 	val &= ~TX_CFG_DMA_EN;
3752 	writel(val, cp->regs + REG_TX_CFG);
3753 
3754 	val = readl(cp->regs + REG_RX_CFG);
3755 	val &= ~RX_CFG_DMA_EN;
3756 	writel(val, cp->regs + REG_RX_CFG);
3757 
3758 	/* program header parser */
3759 	if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
3760 	    (&CAS_HP_ALT_FIRMWARE[0] == &cas_prog_null[0])) {
3761 		cas_load_firmware(cp, CAS_HP_FIRMWARE);
3762 	} else {
3763 		cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
3764 	}
3765 
3766 	/* clear out error registers */
3767 	spin_lock(&cp->stat_lock[N_TX_RINGS]);
3768 	cas_clear_mac_err(cp);
3769 	spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3770 }
3771 
3772 /* Shut down the chip, must be called with pm_mutex held.  */
3773 static void cas_shutdown(struct cas *cp)
3774 {
3775 	unsigned long flags;
3776 
3777 	/* Make us not-running to avoid timers respawning */
3778 	cp->hw_running = 0;
3779 
3780 	timer_delete_sync(&cp->link_timer);
3781 
3782 	/* Stop the reset task */
3783 #if 0
3784 	while (atomic_read(&cp->reset_task_pending_mtu) ||
3785 	       atomic_read(&cp->reset_task_pending_spare) ||
3786 	       atomic_read(&cp->reset_task_pending_all))
3787 		schedule();
3788 
3789 #else
3790 	while (atomic_read(&cp->reset_task_pending))
3791 		schedule();
3792 #endif
3793 	/* Actually stop the chip */
3794 	cas_lock_all_save(cp, flags);
3795 	cas_reset(cp, 0);
3796 	if (cp->cas_flags & CAS_FLAG_SATURN)
3797 		cas_phy_powerdown(cp);
3798 	cas_unlock_all_restore(cp, flags);
3799 }
3800 
3801 static int cas_change_mtu(struct net_device *dev, int new_mtu)
3802 {
3803 	struct cas *cp = netdev_priv(dev);
3804 
3805 	WRITE_ONCE(dev->mtu, new_mtu);
3806 	if (!netif_running(dev) || !netif_device_present(dev))
3807 		return 0;
3808 
3809 	/* let the reset task handle it */
3810 #if 1
3811 	atomic_inc(&cp->reset_task_pending);
3812 	if ((cp->phy_type & CAS_PHY_SERDES)) {
3813 		atomic_inc(&cp->reset_task_pending_all);
3814 	} else {
3815 		atomic_inc(&cp->reset_task_pending_mtu);
3816 	}
3817 	schedule_work(&cp->reset_task);
3818 #else
3819 	atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
3820 		   CAS_RESET_ALL : CAS_RESET_MTU);
3821 	pr_err("reset called in cas_change_mtu\n");
3822 	schedule_work(&cp->reset_task);
3823 #endif
3824 
3825 	flush_work(&cp->reset_task);
3826 	return 0;
3827 }
3828 
3829 static void cas_clean_txd(struct cas *cp, int ring)
3830 {
3831 	struct cas_tx_desc *txd = cp->init_txds[ring];
3832 	struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
3833 	u64 daddr, dlen;
3834 	int i, size;
3835 
3836 	size = TX_DESC_RINGN_SIZE(ring);
3837 	for (i = 0; i < size; i++) {
3838 		int frag;
3839 
3840 		if (skbs[i] == NULL)
3841 			continue;
3842 
3843 		skb = skbs[i];
3844 		skbs[i] = NULL;
3845 
3846 		for (frag = 0; frag <= skb_shinfo(skb)->nr_frags;  frag++) {
3847 			int ent = i & (size - 1);
3848 
3849 			/* first buffer is never a tiny buffer and so
3850 			 * needs to be unmapped.
3851 			 */
3852 			daddr = le64_to_cpu(txd[ent].buffer);
3853 			dlen  =  CAS_VAL(TX_DESC_BUFLEN,
3854 					 le64_to_cpu(txd[ent].control));
3855 			dma_unmap_page(&cp->pdev->dev, daddr, dlen,
3856 				       DMA_TO_DEVICE);
3857 
3858 			if (frag != skb_shinfo(skb)->nr_frags) {
3859 				i++;
3860 
3861 				/* next buffer might by a tiny buffer.
3862 				 * skip past it.
3863 				 */
3864 				ent = i & (size - 1);
3865 				if (cp->tx_tiny_use[ring][ent].used)
3866 					i++;
3867 			}
3868 		}
3869 		dev_kfree_skb_any(skb);
3870 	}
3871 
3872 	/* zero out tiny buf usage */
3873 	memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
3874 }
3875 
3876 /* freed on close */
3877 static inline void cas_free_rx_desc(struct cas *cp, int ring)
3878 {
3879 	cas_page_t **page = cp->rx_pages[ring];
3880 	int i, size;
3881 
3882 	size = RX_DESC_RINGN_SIZE(ring);
3883 	for (i = 0; i < size; i++) {
3884 		if (page[i]) {
3885 			cas_page_free(cp, page[i]);
3886 			page[i] = NULL;
3887 		}
3888 	}
3889 }
3890 
3891 static void cas_free_rxds(struct cas *cp)
3892 {
3893 	int i;
3894 
3895 	for (i = 0; i < N_RX_DESC_RINGS; i++)
3896 		cas_free_rx_desc(cp, i);
3897 }
3898 
3899 /* Must be invoked under cp->lock. */
3900 static void cas_clean_rings(struct cas *cp)
3901 {
3902 	int i;
3903 
3904 	/* need to clean all tx rings */
3905 	memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
3906 	memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
3907 	for (i = 0; i < N_TX_RINGS; i++)
3908 		cas_clean_txd(cp, i);
3909 
3910 	/* zero out init block */
3911 	memset(cp->init_block, 0, sizeof(struct cas_init_block));
3912 	cas_clean_rxds(cp);
3913 	cas_clean_rxcs(cp);
3914 }
3915 
3916 /* allocated on open */
3917 static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
3918 {
3919 	cas_page_t **page = cp->rx_pages[ring];
3920 	int size, i = 0;
3921 
3922 	size = RX_DESC_RINGN_SIZE(ring);
3923 	for (i = 0; i < size; i++) {
3924 		if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
3925 			return -1;
3926 	}
3927 	return 0;
3928 }
3929 
3930 static int cas_alloc_rxds(struct cas *cp)
3931 {
3932 	int i;
3933 
3934 	for (i = 0; i < N_RX_DESC_RINGS; i++) {
3935 		if (cas_alloc_rx_desc(cp, i) < 0) {
3936 			cas_free_rxds(cp);
3937 			return -1;
3938 		}
3939 	}
3940 	return 0;
3941 }
3942 
3943 static void cas_reset_task(struct work_struct *work)
3944 {
3945 	struct cas *cp = container_of(work, struct cas, reset_task);
3946 #if 0
3947 	int pending = atomic_read(&cp->reset_task_pending);
3948 #else
3949 	int pending_all = atomic_read(&cp->reset_task_pending_all);
3950 	int pending_spare = atomic_read(&cp->reset_task_pending_spare);
3951 	int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
3952 
3953 	if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
3954 		/* We can have more tasks scheduled than actually
3955 		 * needed.
3956 		 */
3957 		atomic_dec(&cp->reset_task_pending);
3958 		return;
3959 	}
3960 #endif
3961 	/* The link went down, we reset the ring, but keep
3962 	 * DMA stopped. Use this function for reset
3963 	 * on error as well.
3964 	 */
3965 	if (cp->hw_running) {
3966 		unsigned long flags;
3967 
3968 		/* Make sure we don't get interrupts or tx packets */
3969 		netif_device_detach(cp->dev);
3970 		cas_lock_all_save(cp, flags);
3971 
3972 		if (cp->opened) {
3973 			/* We call cas_spare_recover when we call cas_open.
3974 			 * but we do not initialize the lists cas_spare_recover
3975 			 * uses until cas_open is called.
3976 			 */
3977 			cas_spare_recover(cp, GFP_ATOMIC);
3978 		}
3979 #if 1
3980 		/* test => only pending_spare set */
3981 		if (!pending_all && !pending_mtu)
3982 			goto done;
3983 #else
3984 		if (pending == CAS_RESET_SPARE)
3985 			goto done;
3986 #endif
3987 		/* when pending == CAS_RESET_ALL, the following
3988 		 * call to cas_init_hw will restart auto negotiation.
3989 		 * Setting the second argument of cas_reset to
3990 		 * !(pending == CAS_RESET_ALL) will set this argument
3991 		 * to 1 (avoiding reinitializing the PHY for the normal
3992 		 * PCS case) when auto negotiation is not restarted.
3993 		 */
3994 #if 1
3995 		cas_reset(cp, !(pending_all > 0));
3996 		if (cp->opened)
3997 			cas_clean_rings(cp);
3998 		cas_init_hw(cp, (pending_all > 0));
3999 #else
4000 		cas_reset(cp, !(pending == CAS_RESET_ALL));
4001 		if (cp->opened)
4002 			cas_clean_rings(cp);
4003 		cas_init_hw(cp, pending == CAS_RESET_ALL);
4004 #endif
4005 
4006 done:
4007 		cas_unlock_all_restore(cp, flags);
4008 		netif_device_attach(cp->dev);
4009 	}
4010 #if 1
4011 	atomic_sub(pending_all, &cp->reset_task_pending_all);
4012 	atomic_sub(pending_spare, &cp->reset_task_pending_spare);
4013 	atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
4014 	atomic_dec(&cp->reset_task_pending);
4015 #else
4016 	atomic_set(&cp->reset_task_pending, 0);
4017 #endif
4018 }
4019 
4020 static void cas_link_timer(struct timer_list *t)
4021 {
4022 	struct cas *cp = timer_container_of(cp, t, link_timer);
4023 	int mask, pending = 0, reset = 0;
4024 	unsigned long flags;
4025 
4026 	if (link_transition_timeout != 0 &&
4027 	    cp->link_transition_jiffies_valid &&
4028 	    time_is_before_jiffies(cp->link_transition_jiffies +
4029 	      link_transition_timeout)) {
4030 		/* One-second counter so link-down workaround doesn't
4031 		 * cause resets to occur so fast as to fool the switch
4032 		 * into thinking the link is down.
4033 		 */
4034 		cp->link_transition_jiffies_valid = 0;
4035 	}
4036 
4037 	if (!cp->hw_running)
4038 		return;
4039 
4040 	spin_lock_irqsave(&cp->lock, flags);
4041 	cas_lock_tx(cp);
4042 	cas_entropy_gather(cp);
4043 
4044 	/* If the link task is still pending, we just
4045 	 * reschedule the link timer
4046 	 */
4047 #if 1
4048 	if (atomic_read(&cp->reset_task_pending_all) ||
4049 	    atomic_read(&cp->reset_task_pending_spare) ||
4050 	    atomic_read(&cp->reset_task_pending_mtu))
4051 		goto done;
4052 #else
4053 	if (atomic_read(&cp->reset_task_pending))
4054 		goto done;
4055 #endif
4056 
4057 	/* check for rx cleaning */
4058 	if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
4059 		int i, rmask;
4060 
4061 		for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
4062 			rmask = CAS_FLAG_RXD_POST(i);
4063 			if ((mask & rmask) == 0)
4064 				continue;
4065 
4066 			/* post_rxds will do a mod_timer */
4067 			if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
4068 				pending = 1;
4069 				continue;
4070 			}
4071 			cp->cas_flags &= ~rmask;
4072 		}
4073 	}
4074 
4075 	if (CAS_PHY_MII(cp->phy_type)) {
4076 		u16 bmsr;
4077 		cas_mif_poll(cp, 0);
4078 		bmsr = cas_phy_read(cp, MII_BMSR);
4079 		/* WTZ: Solaris driver reads this twice, but that
4080 		 * may be due to the PCS case and the use of a
4081 		 * common implementation. Read it twice here to be
4082 		 * safe.
4083 		 */
4084 		bmsr = cas_phy_read(cp, MII_BMSR);
4085 		cas_mif_poll(cp, 1);
4086 		readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
4087 		reset = cas_mii_link_check(cp, bmsr);
4088 	} else {
4089 		reset = cas_pcs_link_check(cp);
4090 	}
4091 
4092 	if (reset)
4093 		goto done;
4094 
4095 	/* check for tx state machine confusion */
4096 	if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
4097 		u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
4098 		u32 wptr, rptr;
4099 		int tlm  = CAS_VAL(MAC_SM_TLM, val);
4100 
4101 		if (((tlm == 0x5) || (tlm == 0x3)) &&
4102 		    (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
4103 			netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4104 				     "tx err: MAC_STATE[%08x]\n", val);
4105 			reset = 1;
4106 			goto done;
4107 		}
4108 
4109 		val  = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
4110 		wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
4111 		rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
4112 		if ((val == 0) && (wptr != rptr)) {
4113 			netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4114 				     "tx err: TX_FIFO[%08x:%08x:%08x]\n",
4115 				     val, wptr, rptr);
4116 			reset = 1;
4117 		}
4118 
4119 		if (reset)
4120 			cas_hard_reset(cp);
4121 	}
4122 
4123 done:
4124 	if (reset) {
4125 #if 1
4126 		atomic_inc(&cp->reset_task_pending);
4127 		atomic_inc(&cp->reset_task_pending_all);
4128 		schedule_work(&cp->reset_task);
4129 #else
4130 		atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
4131 		pr_err("reset called in cas_link_timer\n");
4132 		schedule_work(&cp->reset_task);
4133 #endif
4134 	}
4135 
4136 	if (!pending)
4137 		mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
4138 	cas_unlock_tx(cp);
4139 	spin_unlock_irqrestore(&cp->lock, flags);
4140 }
4141 
4142 /* tiny buffers are used to avoid target abort issues with
4143  * older cassini's
4144  */
4145 static void cas_tx_tiny_free(struct cas *cp)
4146 {
4147 	struct pci_dev *pdev = cp->pdev;
4148 	int i;
4149 
4150 	for (i = 0; i < N_TX_RINGS; i++) {
4151 		if (!cp->tx_tiny_bufs[i])
4152 			continue;
4153 
4154 		dma_free_coherent(&pdev->dev, TX_TINY_BUF_BLOCK,
4155 				  cp->tx_tiny_bufs[i], cp->tx_tiny_dvma[i]);
4156 		cp->tx_tiny_bufs[i] = NULL;
4157 	}
4158 }
4159 
4160 static int cas_tx_tiny_alloc(struct cas *cp)
4161 {
4162 	struct pci_dev *pdev = cp->pdev;
4163 	int i;
4164 
4165 	for (i = 0; i < N_TX_RINGS; i++) {
4166 		cp->tx_tiny_bufs[i] =
4167 			dma_alloc_coherent(&pdev->dev, TX_TINY_BUF_BLOCK,
4168 					   &cp->tx_tiny_dvma[i], GFP_KERNEL);
4169 		if (!cp->tx_tiny_bufs[i]) {
4170 			cas_tx_tiny_free(cp);
4171 			return -1;
4172 		}
4173 	}
4174 	return 0;
4175 }
4176 
4177 
4178 static int cas_open(struct net_device *dev)
4179 {
4180 	struct cas *cp = netdev_priv(dev);
4181 	int hw_was_up, err;
4182 	unsigned long flags;
4183 
4184 	mutex_lock(&cp->pm_mutex);
4185 
4186 	hw_was_up = cp->hw_running;
4187 
4188 	/* The power-management mutex protects the hw_running
4189 	 * etc. state so it is safe to do this bit without cp->lock
4190 	 */
4191 	if (!cp->hw_running) {
4192 		/* Reset the chip */
4193 		cas_lock_all_save(cp, flags);
4194 		/* We set the second arg to cas_reset to zero
4195 		 * because cas_init_hw below will have its second
4196 		 * argument set to non-zero, which will force
4197 		 * autonegotiation to start.
4198 		 */
4199 		cas_reset(cp, 0);
4200 		cp->hw_running = 1;
4201 		cas_unlock_all_restore(cp, flags);
4202 	}
4203 
4204 	err = -ENOMEM;
4205 	if (cas_tx_tiny_alloc(cp) < 0)
4206 		goto err_unlock;
4207 
4208 	/* alloc rx descriptors */
4209 	if (cas_alloc_rxds(cp) < 0)
4210 		goto err_tx_tiny;
4211 
4212 	/* allocate spares */
4213 	cas_spare_init(cp);
4214 	cas_spare_recover(cp, GFP_KERNEL);
4215 
4216 	/* We can now request the interrupt as we know it's masked
4217 	 * on the controller. cassini+ has up to 4 interrupts
4218 	 * that can be used, but you need to do explicit pci interrupt
4219 	 * mapping to expose them
4220 	 */
4221 	if (request_irq(cp->pdev->irq, cas_interrupt,
4222 			IRQF_SHARED, dev->name, (void *) dev)) {
4223 		netdev_err(cp->dev, "failed to request irq !\n");
4224 		err = -EAGAIN;
4225 		goto err_spare;
4226 	}
4227 
4228 #ifdef USE_NAPI
4229 	napi_enable(&cp->napi);
4230 #endif
4231 	/* init hw */
4232 	cas_lock_all_save(cp, flags);
4233 	cas_clean_rings(cp);
4234 	cas_init_hw(cp, !hw_was_up);
4235 	cp->opened = 1;
4236 	cas_unlock_all_restore(cp, flags);
4237 
4238 	netif_start_queue(dev);
4239 	mutex_unlock(&cp->pm_mutex);
4240 	return 0;
4241 
4242 err_spare:
4243 	cas_spare_free(cp);
4244 	cas_free_rxds(cp);
4245 err_tx_tiny:
4246 	cas_tx_tiny_free(cp);
4247 err_unlock:
4248 	mutex_unlock(&cp->pm_mutex);
4249 	return err;
4250 }
4251 
4252 static int cas_close(struct net_device *dev)
4253 {
4254 	unsigned long flags;
4255 	struct cas *cp = netdev_priv(dev);
4256 
4257 #ifdef USE_NAPI
4258 	napi_disable(&cp->napi);
4259 #endif
4260 	/* Make sure we don't get distracted by suspend/resume */
4261 	mutex_lock(&cp->pm_mutex);
4262 
4263 	netif_stop_queue(dev);
4264 
4265 	/* Stop traffic, mark us closed */
4266 	cas_lock_all_save(cp, flags);
4267 	cp->opened = 0;
4268 	cas_reset(cp, 0);
4269 	cas_phy_init(cp);
4270 	cas_begin_auto_negotiation(cp, NULL);
4271 	cas_clean_rings(cp);
4272 	cas_unlock_all_restore(cp, flags);
4273 
4274 	free_irq(cp->pdev->irq, (void *) dev);
4275 	cas_spare_free(cp);
4276 	cas_free_rxds(cp);
4277 	cas_tx_tiny_free(cp);
4278 	mutex_unlock(&cp->pm_mutex);
4279 	return 0;
4280 }
4281 
4282 static struct {
4283 	const char name[ETH_GSTRING_LEN];
4284 } ethtool_cassini_statnames[] = {
4285 	{"collisions"},
4286 	{"rx_bytes"},
4287 	{"rx_crc_errors"},
4288 	{"rx_dropped"},
4289 	{"rx_errors"},
4290 	{"rx_fifo_errors"},
4291 	{"rx_frame_errors"},
4292 	{"rx_length_errors"},
4293 	{"rx_over_errors"},
4294 	{"rx_packets"},
4295 	{"tx_aborted_errors"},
4296 	{"tx_bytes"},
4297 	{"tx_dropped"},
4298 	{"tx_errors"},
4299 	{"tx_fifo_errors"},
4300 	{"tx_packets"}
4301 };
4302 #define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
4303 
4304 static struct {
4305 	const int offsets;	/* neg. values for 2nd arg to cas_read_phy */
4306 } ethtool_register_table[] = {
4307 	{-MII_BMSR},
4308 	{-MII_BMCR},
4309 	{REG_CAWR},
4310 	{REG_INF_BURST},
4311 	{REG_BIM_CFG},
4312 	{REG_RX_CFG},
4313 	{REG_HP_CFG},
4314 	{REG_MAC_TX_CFG},
4315 	{REG_MAC_RX_CFG},
4316 	{REG_MAC_CTRL_CFG},
4317 	{REG_MAC_XIF_CFG},
4318 	{REG_MIF_CFG},
4319 	{REG_PCS_CFG},
4320 	{REG_SATURN_PCFG},
4321 	{REG_PCS_MII_STATUS},
4322 	{REG_PCS_STATE_MACHINE},
4323 	{REG_MAC_COLL_EXCESS},
4324 	{REG_MAC_COLL_LATE}
4325 };
4326 #define CAS_REG_LEN 	ARRAY_SIZE(ethtool_register_table)
4327 #define CAS_MAX_REGS 	(sizeof (u32)*CAS_REG_LEN)
4328 
4329 static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
4330 {
4331 	u8 *p;
4332 	int i;
4333 	unsigned long flags;
4334 
4335 	spin_lock_irqsave(&cp->lock, flags);
4336 	for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
4337 		u16 hval;
4338 		u32 val;
4339 		if (ethtool_register_table[i].offsets < 0) {
4340 			hval = cas_phy_read(cp,
4341 				    -ethtool_register_table[i].offsets);
4342 			val = hval;
4343 		} else {
4344 			val= readl(cp->regs+ethtool_register_table[i].offsets);
4345 		}
4346 		memcpy(p, (u8 *)&val, sizeof(u32));
4347 	}
4348 	spin_unlock_irqrestore(&cp->lock, flags);
4349 }
4350 
4351 static struct net_device_stats *cas_get_stats(struct net_device *dev)
4352 {
4353 	struct cas *cp = netdev_priv(dev);
4354 	struct net_device_stats *stats = cp->net_stats;
4355 	unsigned long flags;
4356 	int i;
4357 	unsigned long tmp;
4358 
4359 	/* we collate all of the stats into net_stats[N_TX_RING] */
4360 	if (!cp->hw_running)
4361 		return stats + N_TX_RINGS;
4362 
4363 	/* collect outstanding stats */
4364 	/* WTZ: the Cassini spec gives these as 16 bit counters but
4365 	 * stored in 32-bit words.  Added a mask of 0xffff to be safe,
4366 	 * in case the chip somehow puts any garbage in the other bits.
4367 	 * Also, counter usage didn't seem to mach what Adrian did
4368 	 * in the parts of the code that set these quantities. Made
4369 	 * that consistent.
4370 	 */
4371 	spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
4372 	stats[N_TX_RINGS].rx_crc_errors +=
4373 	  readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
4374 	stats[N_TX_RINGS].rx_frame_errors +=
4375 		readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
4376 	stats[N_TX_RINGS].rx_length_errors +=
4377 		readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
4378 #if 1
4379 	tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
4380 		(readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
4381 	stats[N_TX_RINGS].tx_aborted_errors += tmp;
4382 	stats[N_TX_RINGS].collisions +=
4383 	  tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
4384 #else
4385 	stats[N_TX_RINGS].tx_aborted_errors +=
4386 		readl(cp->regs + REG_MAC_COLL_EXCESS);
4387 	stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
4388 		readl(cp->regs + REG_MAC_COLL_LATE);
4389 #endif
4390 	cas_clear_mac_err(cp);
4391 
4392 	/* saved bits that are unique to ring 0 */
4393 	spin_lock(&cp->stat_lock[0]);
4394 	stats[N_TX_RINGS].collisions        += stats[0].collisions;
4395 	stats[N_TX_RINGS].rx_over_errors    += stats[0].rx_over_errors;
4396 	stats[N_TX_RINGS].rx_frame_errors   += stats[0].rx_frame_errors;
4397 	stats[N_TX_RINGS].rx_fifo_errors    += stats[0].rx_fifo_errors;
4398 	stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
4399 	stats[N_TX_RINGS].tx_fifo_errors    += stats[0].tx_fifo_errors;
4400 	spin_unlock(&cp->stat_lock[0]);
4401 
4402 	for (i = 0; i < N_TX_RINGS; i++) {
4403 		spin_lock(&cp->stat_lock[i]);
4404 		stats[N_TX_RINGS].rx_length_errors +=
4405 			stats[i].rx_length_errors;
4406 		stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
4407 		stats[N_TX_RINGS].rx_packets    += stats[i].rx_packets;
4408 		stats[N_TX_RINGS].tx_packets    += stats[i].tx_packets;
4409 		stats[N_TX_RINGS].rx_bytes      += stats[i].rx_bytes;
4410 		stats[N_TX_RINGS].tx_bytes      += stats[i].tx_bytes;
4411 		stats[N_TX_RINGS].rx_errors     += stats[i].rx_errors;
4412 		stats[N_TX_RINGS].tx_errors     += stats[i].tx_errors;
4413 		stats[N_TX_RINGS].rx_dropped    += stats[i].rx_dropped;
4414 		stats[N_TX_RINGS].tx_dropped    += stats[i].tx_dropped;
4415 		memset(stats + i, 0, sizeof(struct net_device_stats));
4416 		spin_unlock(&cp->stat_lock[i]);
4417 	}
4418 	spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
4419 	return stats + N_TX_RINGS;
4420 }
4421 
4422 
4423 static void cas_set_multicast(struct net_device *dev)
4424 {
4425 	struct cas *cp = netdev_priv(dev);
4426 	u32 rxcfg, rxcfg_new;
4427 	unsigned long flags;
4428 	int limit = STOP_TRIES;
4429 
4430 	if (!cp->hw_running)
4431 		return;
4432 
4433 	spin_lock_irqsave(&cp->lock, flags);
4434 	rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
4435 
4436 	/* disable RX MAC and wait for completion */
4437 	writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4438 	while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
4439 		if (!limit--)
4440 			break;
4441 		udelay(10);
4442 	}
4443 
4444 	/* disable hash filter and wait for completion */
4445 	limit = STOP_TRIES;
4446 	rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
4447 	writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4448 	while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
4449 		if (!limit--)
4450 			break;
4451 		udelay(10);
4452 	}
4453 
4454 	/* program hash filters */
4455 	cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
4456 	rxcfg |= rxcfg_new;
4457 	writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
4458 	spin_unlock_irqrestore(&cp->lock, flags);
4459 }
4460 
4461 static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
4462 {
4463 	struct cas *cp = netdev_priv(dev);
4464 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
4465 	strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
4466 	strscpy(info->bus_info, pci_name(cp->pdev), sizeof(info->bus_info));
4467 }
4468 
4469 static int cas_get_link_ksettings(struct net_device *dev,
4470 				  struct ethtool_link_ksettings *cmd)
4471 {
4472 	struct cas *cp = netdev_priv(dev);
4473 	u16 bmcr;
4474 	int full_duplex, speed, pause;
4475 	unsigned long flags;
4476 	enum link_state linkstate = link_up;
4477 	u32 supported, advertising;
4478 
4479 	advertising = 0;
4480 	supported = SUPPORTED_Autoneg;
4481 	if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
4482 		supported |= SUPPORTED_1000baseT_Full;
4483 		advertising |= ADVERTISED_1000baseT_Full;
4484 	}
4485 
4486 	/* Record PHY settings if HW is on. */
4487 	spin_lock_irqsave(&cp->lock, flags);
4488 	bmcr = 0;
4489 	linkstate = cp->lstate;
4490 	if (CAS_PHY_MII(cp->phy_type)) {
4491 		cmd->base.port = PORT_MII;
4492 		cmd->base.phy_address = cp->phy_addr;
4493 		advertising |= ADVERTISED_TP | ADVERTISED_MII |
4494 			ADVERTISED_10baseT_Half |
4495 			ADVERTISED_10baseT_Full |
4496 			ADVERTISED_100baseT_Half |
4497 			ADVERTISED_100baseT_Full;
4498 
4499 		supported |=
4500 			(SUPPORTED_10baseT_Half |
4501 			 SUPPORTED_10baseT_Full |
4502 			 SUPPORTED_100baseT_Half |
4503 			 SUPPORTED_100baseT_Full |
4504 			 SUPPORTED_TP | SUPPORTED_MII);
4505 
4506 		if (cp->hw_running) {
4507 			cas_mif_poll(cp, 0);
4508 			bmcr = cas_phy_read(cp, MII_BMCR);
4509 			cas_read_mii_link_mode(cp, &full_duplex,
4510 					       &speed, &pause);
4511 			cas_mif_poll(cp, 1);
4512 		}
4513 
4514 	} else {
4515 		cmd->base.port = PORT_FIBRE;
4516 		cmd->base.phy_address = 0;
4517 		supported   |= SUPPORTED_FIBRE;
4518 		advertising |= ADVERTISED_FIBRE;
4519 
4520 		if (cp->hw_running) {
4521 			/* pcs uses the same bits as mii */
4522 			bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
4523 			cas_read_pcs_link_mode(cp, &full_duplex,
4524 					       &speed, &pause);
4525 		}
4526 	}
4527 	spin_unlock_irqrestore(&cp->lock, flags);
4528 
4529 	if (bmcr & BMCR_ANENABLE) {
4530 		advertising |= ADVERTISED_Autoneg;
4531 		cmd->base.autoneg = AUTONEG_ENABLE;
4532 		cmd->base.speed =  ((speed == 10) ?
4533 					    SPEED_10 :
4534 					    ((speed == 1000) ?
4535 					     SPEED_1000 : SPEED_100));
4536 		cmd->base.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
4537 	} else {
4538 		cmd->base.autoneg = AUTONEG_DISABLE;
4539 		cmd->base.speed = ((bmcr & CAS_BMCR_SPEED1000) ?
4540 					    SPEED_1000 :
4541 					    ((bmcr & BMCR_SPEED100) ?
4542 					     SPEED_100 : SPEED_10));
4543 		cmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
4544 			DUPLEX_FULL : DUPLEX_HALF;
4545 	}
4546 	if (linkstate != link_up) {
4547 		/* Force these to "unknown" if the link is not up and
4548 		 * autonogotiation in enabled. We can set the link
4549 		 * speed to 0, but not cmd->duplex,
4550 		 * because its legal values are 0 and 1.  Ethtool will
4551 		 * print the value reported in parentheses after the
4552 		 * word "Unknown" for unrecognized values.
4553 		 *
4554 		 * If in forced mode, we report the speed and duplex
4555 		 * settings that we configured.
4556 		 */
4557 		if (cp->link_cntl & BMCR_ANENABLE) {
4558 			cmd->base.speed = 0;
4559 			cmd->base.duplex = 0xff;
4560 		} else {
4561 			cmd->base.speed = SPEED_10;
4562 			if (cp->link_cntl & BMCR_SPEED100) {
4563 				cmd->base.speed = SPEED_100;
4564 			} else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
4565 				cmd->base.speed = SPEED_1000;
4566 			}
4567 			cmd->base.duplex = (cp->link_cntl & BMCR_FULLDPLX) ?
4568 				DUPLEX_FULL : DUPLEX_HALF;
4569 		}
4570 	}
4571 
4572 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
4573 						supported);
4574 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
4575 						advertising);
4576 
4577 	return 0;
4578 }
4579 
4580 static int cas_set_link_ksettings(struct net_device *dev,
4581 				  const struct ethtool_link_ksettings *cmd)
4582 {
4583 	struct cas *cp = netdev_priv(dev);
4584 	unsigned long flags;
4585 	u32 speed = cmd->base.speed;
4586 
4587 	/* Verify the settings we care about. */
4588 	if (cmd->base.autoneg != AUTONEG_ENABLE &&
4589 	    cmd->base.autoneg != AUTONEG_DISABLE)
4590 		return -EINVAL;
4591 
4592 	if (cmd->base.autoneg == AUTONEG_DISABLE &&
4593 	    ((speed != SPEED_1000 &&
4594 	      speed != SPEED_100 &&
4595 	      speed != SPEED_10) ||
4596 	     (cmd->base.duplex != DUPLEX_HALF &&
4597 	      cmd->base.duplex != DUPLEX_FULL)))
4598 		return -EINVAL;
4599 
4600 	/* Apply settings and restart link process. */
4601 	spin_lock_irqsave(&cp->lock, flags);
4602 	cas_begin_auto_negotiation(cp, cmd);
4603 	spin_unlock_irqrestore(&cp->lock, flags);
4604 	return 0;
4605 }
4606 
4607 static int cas_nway_reset(struct net_device *dev)
4608 {
4609 	struct cas *cp = netdev_priv(dev);
4610 	unsigned long flags;
4611 
4612 	if ((cp->link_cntl & BMCR_ANENABLE) == 0)
4613 		return -EINVAL;
4614 
4615 	/* Restart link process. */
4616 	spin_lock_irqsave(&cp->lock, flags);
4617 	cas_begin_auto_negotiation(cp, NULL);
4618 	spin_unlock_irqrestore(&cp->lock, flags);
4619 
4620 	return 0;
4621 }
4622 
4623 static u32 cas_get_link(struct net_device *dev)
4624 {
4625 	struct cas *cp = netdev_priv(dev);
4626 	return cp->lstate == link_up;
4627 }
4628 
4629 static u32 cas_get_msglevel(struct net_device *dev)
4630 {
4631 	struct cas *cp = netdev_priv(dev);
4632 	return cp->msg_enable;
4633 }
4634 
4635 static void cas_set_msglevel(struct net_device *dev, u32 value)
4636 {
4637 	struct cas *cp = netdev_priv(dev);
4638 	cp->msg_enable = value;
4639 }
4640 
4641 static int cas_get_regs_len(struct net_device *dev)
4642 {
4643 	struct cas *cp = netdev_priv(dev);
4644 	return min_t(int, cp->casreg_len, CAS_MAX_REGS);
4645 }
4646 
4647 static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
4648 			     void *p)
4649 {
4650 	struct cas *cp = netdev_priv(dev);
4651 	regs->version = 0;
4652 	/* cas_read_regs handles locks (cp->lock).  */
4653 	cas_read_regs(cp, p, regs->len / sizeof(u32));
4654 }
4655 
4656 static int cas_get_sset_count(struct net_device *dev, int sset)
4657 {
4658 	switch (sset) {
4659 	case ETH_SS_STATS:
4660 		return CAS_NUM_STAT_KEYS;
4661 	default:
4662 		return -EOPNOTSUPP;
4663 	}
4664 }
4665 
4666 static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4667 {
4668 	 memcpy(data, &ethtool_cassini_statnames,
4669 					 CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
4670 }
4671 
4672 static void cas_get_ethtool_stats(struct net_device *dev,
4673 				      struct ethtool_stats *estats, u64 *data)
4674 {
4675 	struct cas *cp = netdev_priv(dev);
4676 	struct net_device_stats *stats = cas_get_stats(cp->dev);
4677 	int i = 0;
4678 	data[i++] = stats->collisions;
4679 	data[i++] = stats->rx_bytes;
4680 	data[i++] = stats->rx_crc_errors;
4681 	data[i++] = stats->rx_dropped;
4682 	data[i++] = stats->rx_errors;
4683 	data[i++] = stats->rx_fifo_errors;
4684 	data[i++] = stats->rx_frame_errors;
4685 	data[i++] = stats->rx_length_errors;
4686 	data[i++] = stats->rx_over_errors;
4687 	data[i++] = stats->rx_packets;
4688 	data[i++] = stats->tx_aborted_errors;
4689 	data[i++] = stats->tx_bytes;
4690 	data[i++] = stats->tx_dropped;
4691 	data[i++] = stats->tx_errors;
4692 	data[i++] = stats->tx_fifo_errors;
4693 	data[i++] = stats->tx_packets;
4694 	BUG_ON(i != CAS_NUM_STAT_KEYS);
4695 }
4696 
4697 static const struct ethtool_ops cas_ethtool_ops = {
4698 	.get_drvinfo		= cas_get_drvinfo,
4699 	.nway_reset		= cas_nway_reset,
4700 	.get_link		= cas_get_link,
4701 	.get_msglevel		= cas_get_msglevel,
4702 	.set_msglevel		= cas_set_msglevel,
4703 	.get_regs_len		= cas_get_regs_len,
4704 	.get_regs		= cas_get_regs,
4705 	.get_sset_count		= cas_get_sset_count,
4706 	.get_strings		= cas_get_strings,
4707 	.get_ethtool_stats	= cas_get_ethtool_stats,
4708 	.get_link_ksettings	= cas_get_link_ksettings,
4709 	.set_link_ksettings	= cas_set_link_ksettings,
4710 };
4711 
4712 static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4713 {
4714 	struct cas *cp = netdev_priv(dev);
4715 	struct mii_ioctl_data *data = if_mii(ifr);
4716 	unsigned long flags;
4717 	int rc = -EOPNOTSUPP;
4718 
4719 	/* Hold the PM mutex while doing ioctl's or we may collide
4720 	 * with open/close and power management and oops.
4721 	 */
4722 	mutex_lock(&cp->pm_mutex);
4723 	switch (cmd) {
4724 	case SIOCGMIIPHY:		/* Get address of MII PHY in use. */
4725 		data->phy_id = cp->phy_addr;
4726 		fallthrough;
4727 
4728 	case SIOCGMIIREG:		/* Read MII PHY register. */
4729 		spin_lock_irqsave(&cp->lock, flags);
4730 		cas_mif_poll(cp, 0);
4731 		data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
4732 		cas_mif_poll(cp, 1);
4733 		spin_unlock_irqrestore(&cp->lock, flags);
4734 		rc = 0;
4735 		break;
4736 
4737 	case SIOCSMIIREG:		/* Write MII PHY register. */
4738 		spin_lock_irqsave(&cp->lock, flags);
4739 		cas_mif_poll(cp, 0);
4740 		rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
4741 		cas_mif_poll(cp, 1);
4742 		spin_unlock_irqrestore(&cp->lock, flags);
4743 		break;
4744 	default:
4745 		break;
4746 	}
4747 
4748 	mutex_unlock(&cp->pm_mutex);
4749 	return rc;
4750 }
4751 
4752 /* When this chip sits underneath an Intel 31154 bridge, it is the
4753  * only subordinate device and we can tweak the bridge settings to
4754  * reflect that fact.
4755  */
4756 static void cas_program_bridge(struct pci_dev *cas_pdev)
4757 {
4758 	struct pci_dev *pdev = cas_pdev->bus->self;
4759 	u32 val;
4760 
4761 	if (!pdev)
4762 		return;
4763 
4764 	if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
4765 		return;
4766 
4767 	/* Clear bit 10 (Bus Parking Control) in the Secondary
4768 	 * Arbiter Control/Status Register which lives at offset
4769 	 * 0x41.  Using a 32-bit word read/modify/write at 0x40
4770 	 * is much simpler so that's how we do this.
4771 	 */
4772 	pci_read_config_dword(pdev, 0x40, &val);
4773 	val &= ~0x00040000;
4774 	pci_write_config_dword(pdev, 0x40, val);
4775 
4776 	/* Max out the Multi-Transaction Timer settings since
4777 	 * Cassini is the only device present.
4778 	 *
4779 	 * The register is 16-bit and lives at 0x50.  When the
4780 	 * settings are enabled, it extends the GRANT# signal
4781 	 * for a requestor after a transaction is complete.  This
4782 	 * allows the next request to run without first needing
4783 	 * to negotiate the GRANT# signal back.
4784 	 *
4785 	 * Bits 12:10 define the grant duration:
4786 	 *
4787 	 *	1	--	16 clocks
4788 	 *	2	--	32 clocks
4789 	 *	3	--	64 clocks
4790 	 *	4	--	128 clocks
4791 	 *	5	--	256 clocks
4792 	 *
4793 	 * All other values are illegal.
4794 	 *
4795 	 * Bits 09:00 define which REQ/GNT signal pairs get the
4796 	 * GRANT# signal treatment.  We set them all.
4797 	 */
4798 	pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
4799 
4800 	/* The Read Prefecth Policy register is 16-bit and sits at
4801 	 * offset 0x52.  It enables a "smart" pre-fetch policy.  We
4802 	 * enable it and max out all of the settings since only one
4803 	 * device is sitting underneath and thus bandwidth sharing is
4804 	 * not an issue.
4805 	 *
4806 	 * The register has several 3 bit fields, which indicates a
4807 	 * multiplier applied to the base amount of prefetching the
4808 	 * chip would do.  These fields are at:
4809 	 *
4810 	 *	15:13	---	ReRead Primary Bus
4811 	 *	12:10	---	FirstRead Primary Bus
4812 	 *	09:07	---	ReRead Secondary Bus
4813 	 *	06:04	---	FirstRead Secondary Bus
4814 	 *
4815 	 * Bits 03:00 control which REQ/GNT pairs the prefetch settings
4816 	 * get enabled on.  Bit 3 is a grouped enabler which controls
4817 	 * all of the REQ/GNT pairs from [8:3].  Bits 2 to 0 control
4818 	 * the individual REQ/GNT pairs [2:0].
4819 	 */
4820 	pci_write_config_word(pdev, 0x52,
4821 			      (0x7 << 13) |
4822 			      (0x7 << 10) |
4823 			      (0x7 <<  7) |
4824 			      (0x7 <<  4) |
4825 			      (0xf <<  0));
4826 
4827 	/* Force cacheline size to 0x8 */
4828 	pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4829 
4830 	/* Force latency timer to maximum setting so Cassini can
4831 	 * sit on the bus as long as it likes.
4832 	 */
4833 	pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
4834 }
4835 
4836 static const struct net_device_ops cas_netdev_ops = {
4837 	.ndo_open		= cas_open,
4838 	.ndo_stop		= cas_close,
4839 	.ndo_start_xmit		= cas_start_xmit,
4840 	.ndo_get_stats 		= cas_get_stats,
4841 	.ndo_set_rx_mode	= cas_set_multicast,
4842 	.ndo_eth_ioctl		= cas_ioctl,
4843 	.ndo_tx_timeout		= cas_tx_timeout,
4844 	.ndo_change_mtu		= cas_change_mtu,
4845 	.ndo_set_mac_address	= eth_mac_addr,
4846 	.ndo_validate_addr	= eth_validate_addr,
4847 #ifdef CONFIG_NET_POLL_CONTROLLER
4848 	.ndo_poll_controller	= cas_netpoll,
4849 #endif
4850 };
4851 
4852 static int cas_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4853 {
4854 	static int cas_version_printed = 0;
4855 	unsigned long casreg_len;
4856 	struct net_device *dev;
4857 	struct cas *cp;
4858 	u16 pci_cmd;
4859 	int i, err;
4860 	u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
4861 
4862 	if (cas_version_printed++ == 0)
4863 		pr_info("%s", version);
4864 
4865 	err = pci_enable_device(pdev);
4866 	if (err) {
4867 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
4868 		return err;
4869 	}
4870 
4871 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
4872 		dev_err(&pdev->dev, "Cannot find proper PCI device "
4873 		       "base address, aborting\n");
4874 		err = -ENODEV;
4875 		goto err_out_disable_pdev;
4876 	}
4877 
4878 	dev = alloc_etherdev(sizeof(*cp));
4879 	if (!dev) {
4880 		err = -ENOMEM;
4881 		goto err_out_disable_pdev;
4882 	}
4883 	SET_NETDEV_DEV(dev, &pdev->dev);
4884 
4885 	err = pci_request_regions(pdev, dev->name);
4886 	if (err) {
4887 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
4888 		goto err_out_free_netdev;
4889 	}
4890 	pci_set_master(pdev);
4891 
4892 	/* we must always turn on parity response or else parity
4893 	 * doesn't get generated properly. disable SERR/PERR as well.
4894 	 * in addition, we want to turn MWI on.
4895 	 */
4896 	pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4897 	pci_cmd &= ~PCI_COMMAND_SERR;
4898 	pci_cmd |= PCI_COMMAND_PARITY;
4899 	pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4900 	if (pci_try_set_mwi(pdev))
4901 		pr_warn("Could not enable MWI for %s\n", pci_name(pdev));
4902 
4903 	cas_program_bridge(pdev);
4904 
4905 	/*
4906 	 * On some architectures, the default cache line size set
4907 	 * by pci_try_set_mwi reduces perforamnce.  We have to increase
4908 	 * it for this case.  To start, we'll print some configuration
4909 	 * data.
4910 	 */
4911 #if 1
4912 	pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
4913 			     &orig_cacheline_size);
4914 	if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
4915 		cas_cacheline_size =
4916 			(CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
4917 			CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
4918 		if (pci_write_config_byte(pdev,
4919 					  PCI_CACHE_LINE_SIZE,
4920 					  cas_cacheline_size)) {
4921 			dev_err(&pdev->dev, "Could not set PCI cache "
4922 			       "line size\n");
4923 			goto err_out_free_res;
4924 		}
4925 	}
4926 #endif
4927 
4928 
4929 	/* Configure DMA attributes. */
4930 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4931 	if (err) {
4932 		dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4933 		goto err_out_free_res;
4934 	}
4935 
4936 	casreg_len = pci_resource_len(pdev, 0);
4937 
4938 	cp = netdev_priv(dev);
4939 	cp->pdev = pdev;
4940 #if 1
4941 	/* A value of 0 indicates we never explicitly set it */
4942 	cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
4943 #endif
4944 	cp->dev = dev;
4945 	cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
4946 	  cassini_debug;
4947 
4948 #if defined(CONFIG_SPARC)
4949 	cp->of_node = pci_device_to_OF_node(pdev);
4950 #endif
4951 
4952 	cp->link_transition = LINK_TRANSITION_UNKNOWN;
4953 	cp->link_transition_jiffies_valid = 0;
4954 
4955 	spin_lock_init(&cp->lock);
4956 	spin_lock_init(&cp->rx_inuse_lock);
4957 	spin_lock_init(&cp->rx_spare_lock);
4958 	for (i = 0; i < N_TX_RINGS; i++) {
4959 		spin_lock_init(&cp->stat_lock[i]);
4960 		spin_lock_init(&cp->tx_lock[i]);
4961 	}
4962 	spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
4963 	mutex_init(&cp->pm_mutex);
4964 
4965 	timer_setup(&cp->link_timer, cas_link_timer, 0);
4966 
4967 #if 1
4968 	/* Just in case the implementation of atomic operations
4969 	 * change so that an explicit initialization is necessary.
4970 	 */
4971 	atomic_set(&cp->reset_task_pending, 0);
4972 	atomic_set(&cp->reset_task_pending_all, 0);
4973 	atomic_set(&cp->reset_task_pending_spare, 0);
4974 	atomic_set(&cp->reset_task_pending_mtu, 0);
4975 #endif
4976 	INIT_WORK(&cp->reset_task, cas_reset_task);
4977 
4978 	/* Default link parameters */
4979 	if (link_mode >= 0 && link_mode < 6)
4980 		cp->link_cntl = link_modes[link_mode];
4981 	else
4982 		cp->link_cntl = BMCR_ANENABLE;
4983 	cp->lstate = link_down;
4984 	cp->link_transition = LINK_TRANSITION_LINK_DOWN;
4985 	netif_carrier_off(cp->dev);
4986 	cp->timer_ticks = 0;
4987 
4988 	/* give us access to cassini registers */
4989 	cp->regs = pci_iomap(pdev, 0, casreg_len);
4990 	if (!cp->regs) {
4991 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
4992 		goto err_out_free_res;
4993 	}
4994 	cp->casreg_len = casreg_len;
4995 
4996 	pci_save_state(pdev);
4997 	cas_check_pci_invariants(cp);
4998 	cas_hard_reset(cp);
4999 	cas_reset(cp, 0);
5000 	if (cas_check_invariants(cp))
5001 		goto err_out_iounmap;
5002 	if (cp->cas_flags & CAS_FLAG_SATURN)
5003 		cas_saturn_firmware_init(cp);
5004 
5005 	cp->init_block =
5006 		dma_alloc_coherent(&pdev->dev, sizeof(struct cas_init_block),
5007 				   &cp->block_dvma, GFP_KERNEL);
5008 	if (!cp->init_block) {
5009 		dev_err(&pdev->dev, "Cannot allocate init block, aborting\n");
5010 		goto err_out_iounmap;
5011 	}
5012 
5013 	for (i = 0; i < N_TX_RINGS; i++)
5014 		cp->init_txds[i] = cp->init_block->txds[i];
5015 
5016 	for (i = 0; i < N_RX_DESC_RINGS; i++)
5017 		cp->init_rxds[i] = cp->init_block->rxds[i];
5018 
5019 	for (i = 0; i < N_RX_COMP_RINGS; i++)
5020 		cp->init_rxcs[i] = cp->init_block->rxcs[i];
5021 
5022 	for (i = 0; i < N_RX_FLOWS; i++)
5023 		skb_queue_head_init(&cp->rx_flows[i]);
5024 
5025 	dev->netdev_ops = &cas_netdev_ops;
5026 	dev->ethtool_ops = &cas_ethtool_ops;
5027 	dev->watchdog_timeo = CAS_TX_TIMEOUT;
5028 
5029 #ifdef USE_NAPI
5030 	netif_napi_add(dev, &cp->napi, cas_poll);
5031 #endif
5032 	dev->irq = pdev->irq;
5033 	dev->dma = 0;
5034 
5035 	/* Cassini features. */
5036 	if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
5037 		dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5038 
5039 	dev->features |= NETIF_F_HIGHDMA;
5040 
5041 	/* MTU range: 60 - varies or 9000 */
5042 	dev->min_mtu = CAS_MIN_MTU;
5043 	dev->max_mtu = CAS_MAX_MTU;
5044 
5045 	if (register_netdev(dev)) {
5046 		dev_err(&pdev->dev, "Cannot register net device, aborting\n");
5047 		goto err_out_free_consistent;
5048 	}
5049 
5050 	i = readl(cp->regs + REG_BIM_CFG);
5051 	netdev_info(dev, "Sun Cassini%s (%sbit/%sMHz PCI/%s) Ethernet[%d] %pM\n",
5052 		    (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
5053 		    (i & BIM_CFG_32BIT) ? "32" : "64",
5054 		    (i & BIM_CFG_66MHZ) ? "66" : "33",
5055 		    (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
5056 		    dev->dev_addr);
5057 
5058 	pci_set_drvdata(pdev, dev);
5059 	cp->hw_running = 1;
5060 	cas_entropy_reset(cp);
5061 	cas_phy_init(cp);
5062 	cas_begin_auto_negotiation(cp, NULL);
5063 	return 0;
5064 
5065 err_out_free_consistent:
5066 	dma_free_coherent(&pdev->dev, sizeof(struct cas_init_block),
5067 			  cp->init_block, cp->block_dvma);
5068 
5069 err_out_iounmap:
5070 	mutex_lock(&cp->pm_mutex);
5071 	if (cp->hw_running)
5072 		cas_shutdown(cp);
5073 	mutex_unlock(&cp->pm_mutex);
5074 
5075 	vfree(cp->fw_data);
5076 
5077 	pci_iounmap(pdev, cp->regs);
5078 
5079 
5080 err_out_free_res:
5081 	pci_release_regions(pdev);
5082 
5083 	/* Try to restore it in case the error occurred after we
5084 	 * set it.
5085 	 */
5086 	pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
5087 
5088 err_out_free_netdev:
5089 	free_netdev(dev);
5090 
5091 err_out_disable_pdev:
5092 	pci_disable_device(pdev);
5093 	return -ENODEV;
5094 }
5095 
5096 static void cas_remove_one(struct pci_dev *pdev)
5097 {
5098 	struct net_device *dev = pci_get_drvdata(pdev);
5099 	struct cas *cp;
5100 	if (!dev)
5101 		return;
5102 
5103 	cp = netdev_priv(dev);
5104 	unregister_netdev(dev);
5105 
5106 	vfree(cp->fw_data);
5107 
5108 	mutex_lock(&cp->pm_mutex);
5109 	cancel_work_sync(&cp->reset_task);
5110 	if (cp->hw_running)
5111 		cas_shutdown(cp);
5112 	mutex_unlock(&cp->pm_mutex);
5113 
5114 #if 1
5115 	if (cp->orig_cacheline_size) {
5116 		/* Restore the cache line size if we had modified
5117 		 * it.
5118 		 */
5119 		pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5120 				      cp->orig_cacheline_size);
5121 	}
5122 #endif
5123 	dma_free_coherent(&pdev->dev, sizeof(struct cas_init_block),
5124 			  cp->init_block, cp->block_dvma);
5125 	pci_iounmap(pdev, cp->regs);
5126 	free_netdev(dev);
5127 	pci_release_regions(pdev);
5128 	pci_disable_device(pdev);
5129 }
5130 
5131 static int __maybe_unused cas_suspend(struct device *dev_d)
5132 {
5133 	struct net_device *dev = dev_get_drvdata(dev_d);
5134 	struct cas *cp = netdev_priv(dev);
5135 	unsigned long flags;
5136 
5137 	mutex_lock(&cp->pm_mutex);
5138 
5139 	/* If the driver is opened, we stop the DMA */
5140 	if (cp->opened) {
5141 		netif_device_detach(dev);
5142 
5143 		cas_lock_all_save(cp, flags);
5144 
5145 		/* We can set the second arg of cas_reset to 0
5146 		 * because on resume, we'll call cas_init_hw with
5147 		 * its second arg set so that autonegotiation is
5148 		 * restarted.
5149 		 */
5150 		cas_reset(cp, 0);
5151 		cas_clean_rings(cp);
5152 		cas_unlock_all_restore(cp, flags);
5153 	}
5154 
5155 	if (cp->hw_running)
5156 		cas_shutdown(cp);
5157 	mutex_unlock(&cp->pm_mutex);
5158 
5159 	return 0;
5160 }
5161 
5162 static int __maybe_unused cas_resume(struct device *dev_d)
5163 {
5164 	struct net_device *dev = dev_get_drvdata(dev_d);
5165 	struct cas *cp = netdev_priv(dev);
5166 
5167 	netdev_info(dev, "resuming\n");
5168 
5169 	mutex_lock(&cp->pm_mutex);
5170 	cas_hard_reset(cp);
5171 	if (cp->opened) {
5172 		unsigned long flags;
5173 		cas_lock_all_save(cp, flags);
5174 		cas_reset(cp, 0);
5175 		cp->hw_running = 1;
5176 		cas_clean_rings(cp);
5177 		cas_init_hw(cp, 1);
5178 		cas_unlock_all_restore(cp, flags);
5179 
5180 		netif_device_attach(dev);
5181 	}
5182 	mutex_unlock(&cp->pm_mutex);
5183 	return 0;
5184 }
5185 
5186 static SIMPLE_DEV_PM_OPS(cas_pm_ops, cas_suspend, cas_resume);
5187 
5188 static struct pci_driver cas_driver = {
5189 	.name		= DRV_MODULE_NAME,
5190 	.id_table	= cas_pci_tbl,
5191 	.probe		= cas_init_one,
5192 	.remove		= cas_remove_one,
5193 	.driver.pm	= &cas_pm_ops,
5194 };
5195 
5196 static int __init cas_init(void)
5197 {
5198 	if (linkdown_timeout > 0)
5199 		link_transition_timeout = linkdown_timeout * HZ;
5200 	else
5201 		link_transition_timeout = 0;
5202 
5203 	return pci_register_driver(&cas_driver);
5204 }
5205 
5206 static void __exit cas_cleanup(void)
5207 {
5208 	pci_unregister_driver(&cas_driver);
5209 }
5210 
5211 module_init(cas_init);
5212 module_exit(cas_cleanup);
5213