1 // SPDX-License-Identifier: GPL-2.0+
2 /* cassini.c: Sun Microsystems Cassini(+) ethernet driver.
4 * Copyright (C) 2004 Sun Microsystems Inc.
7 * This driver uses the sungem driver (c) David Miller
10 * The cassini chip has a number of features that distinguish it from
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
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
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)
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
34 * NOTE: cassini can parse the header. however, it's not worth it
35 * as long as the network stack requires a header copy.
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
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
52 * by default, the selective clear mask is set up to process rx packets.
55 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
69 #include <linux/highmem.h>
70 #include <linux/list.h>
71 #include <linux/dma-mapping.h>
73 #include <linux/netdevice.h>
74 #include <linux/etherdevice.h>
75 #include <linux/skbuff.h>
76 #include <linux/ethtool.h>
77 #include <linux/crc32.h>
78 #include <linux/random.h>
79 #include <linux/mii.h>
81 #include <linux/tcp.h>
82 #include <linux/mutex.h>
83 #include <linux/firmware.h>
85 #include <net/checksum.h>
87 #include <linux/atomic.h>
89 #include <asm/byteorder.h>
90 #include <linux/uaccess.h>
91 #include <linux/jiffies.h>
93 #define CAS_NCPUS num_online_cpus()
95 #define cas_skb_release(x) netif_rx(x)
97 /* select which firmware to use */
98 #define USE_HP_WORKAROUND
99 #define HP_WORKAROUND_DEFAULT /* select which firmware to use as default */
100 #define CAS_HP_ALT_FIRMWARE cas_prog_null /* alternate firmware */
104 #define USE_TX_COMPWB /* use completion writeback registers */
105 #define USE_CSMA_CD_PROTO /* standard CSMA/CD */
106 #define USE_RX_BLANK /* hw interrupt mitigation */
107 #undef USE_ENTROPY_DEV /* don't test for entropy device */
109 /* NOTE: these aren't useable unless PCI interrupts can be assigned.
110 * also, we need to make cp->lock finer-grained.
117 #undef USE_VPD_DEBUG /* debug vpd information if defined */
119 /* rx processing options */
120 #define USE_PAGE_ORDER /* specify to allocate large rx pages */
121 #define RX_DONT_BATCH 0 /* if 1, don't batch flows */
122 #define RX_COPY_ALWAYS 0 /* if 0, use frags */
123 #define RX_COPY_MIN 64 /* copy a little to make upper layers happy */
124 #undef RX_COUNT_BUFFERS /* define to calculate RX buffer stats */
126 #define DRV_MODULE_NAME "cassini"
127 #define DRV_MODULE_VERSION "1.6"
128 #define DRV_MODULE_RELDATE "21 May 2008"
130 #define CAS_DEF_MSG_ENABLE \
140 /* length of time before we decide the hardware is borked,
141 * and dev->tx_timeout() should be called to fix the problem
143 #define CAS_TX_TIMEOUT (HZ)
144 #define CAS_LINK_TIMEOUT (22*HZ/10)
145 #define CAS_LINK_FAST_TIMEOUT (1)
147 /* timeout values for state changing. these specify the number
148 * of 10us delays to be used before giving up.
150 #define STOP_TRIES_PHY 1000
151 #define STOP_TRIES 5000
153 /* specify a minimum frame size to deal with some fifo issues
154 * max mtu == 2 * page size - ethernet header - 64 - swivel =
155 * 2 * page_size - 0x50
157 #define CAS_MIN_FRAME 97
158 #define CAS_1000MB_MIN_FRAME 255
159 #define CAS_MIN_MTU 60
160 #define CAS_MAX_MTU min(((cp->page_size << 1) - 0x50), 9000)
164 * Eliminate these and use separate atomic counters for each, to
165 * avoid a race condition.
168 #define CAS_RESET_MTU 1
169 #define CAS_RESET_ALL 2
170 #define CAS_RESET_SPARE 3
173 static char version[] =
174 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
176 static int cassini_debug = -1; /* -1 == use CAS_DEF_MSG_ENABLE as value */
177 static int link_mode;
180 MODULE_DESCRIPTION("Sun Cassini(+) ethernet driver");
181 MODULE_LICENSE("GPL");
182 MODULE_FIRMWARE("sun/cassini.bin");
183 module_param(cassini_debug, int, 0);
184 MODULE_PARM_DESC(cassini_debug, "Cassini bitmapped debugging message enable value");
185 module_param(link_mode, int, 0);
186 MODULE_PARM_DESC(link_mode, "default link mode");
189 * Work around for a PCS bug in which the link goes down due to the chip
190 * being confused and never showing a link status of "up."
192 #define DEFAULT_LINKDOWN_TIMEOUT 5
194 * Value in seconds, for user input.
196 static int linkdown_timeout = DEFAULT_LINKDOWN_TIMEOUT;
197 module_param(linkdown_timeout, int, 0);
198 MODULE_PARM_DESC(linkdown_timeout,
199 "min reset interval in sec. for PCS linkdown issue; disabled if not positive");
202 * value in 'ticks' (units used by jiffies). Set when we init the
203 * module because 'HZ' in actually a function call on some flavors of
204 * Linux. This will default to DEFAULT_LINKDOWN_TIMEOUT * HZ.
206 static int link_transition_timeout;
210 static u16 link_modes[] = {
211 BMCR_ANENABLE, /* 0 : autoneg */
212 0, /* 1 : 10bt half duplex */
213 BMCR_SPEED100, /* 2 : 100bt half duplex */
214 BMCR_FULLDPLX, /* 3 : 10bt full duplex */
215 BMCR_SPEED100|BMCR_FULLDPLX, /* 4 : 100bt full duplex */
216 CAS_BMCR_SPEED1000|BMCR_FULLDPLX /* 5 : 1000bt full duplex */
219 static const struct pci_device_id cas_pci_tbl[] = {
220 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_CASSINI,
221 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
222 { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SATURN,
223 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
227 MODULE_DEVICE_TABLE(pci, cas_pci_tbl);
229 static void cas_set_link_modes(struct cas *cp);
231 static inline void cas_lock_tx(struct cas *cp)
235 for (i = 0; i < N_TX_RINGS; i++)
236 spin_lock_nested(&cp->tx_lock[i], i);
239 /* WTZ: QA was finding deadlock problems with the previous
240 * versions after long test runs with multiple cards per machine.
241 * See if replacing cas_lock_all with safer versions helps. The
242 * symptoms QA is reporting match those we'd expect if interrupts
243 * aren't being properly restored, and we fixed a previous deadlock
244 * with similar symptoms by using save/restore versions in other
247 #define cas_lock_all_save(cp, flags) \
249 struct cas *xxxcp = (cp); \
250 spin_lock_irqsave(&xxxcp->lock, flags); \
251 cas_lock_tx(xxxcp); \
254 static inline void cas_unlock_tx(struct cas *cp)
258 for (i = N_TX_RINGS; i > 0; i--)
259 spin_unlock(&cp->tx_lock[i - 1]);
262 #define cas_unlock_all_restore(cp, flags) \
264 struct cas *xxxcp = (cp); \
265 cas_unlock_tx(xxxcp); \
266 spin_unlock_irqrestore(&xxxcp->lock, flags); \
269 static void cas_disable_irq(struct cas *cp, const int ring)
271 /* Make sure we won't get any more interrupts */
273 writel(0xFFFFFFFF, cp->regs + REG_INTR_MASK);
277 /* disable completion interrupts and selectively mask */
278 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
280 #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
290 writel(INTRN_MASK_CLEAR_ALL | INTRN_MASK_RX_EN,
291 cp->regs + REG_PLUS_INTRN_MASK(ring));
295 writel(INTRN_MASK_CLEAR_ALL, cp->regs +
296 REG_PLUS_INTRN_MASK(ring));
302 static inline void cas_mask_intr(struct cas *cp)
306 for (i = 0; i < N_RX_COMP_RINGS; i++)
307 cas_disable_irq(cp, i);
310 static void cas_enable_irq(struct cas *cp, const int ring)
312 if (ring == 0) { /* all but TX_DONE */
313 writel(INTR_TX_DONE, cp->regs + REG_INTR_MASK);
317 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
319 #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
329 writel(INTRN_MASK_RX_EN, cp->regs +
330 REG_PLUS_INTRN_MASK(ring));
339 static inline void cas_unmask_intr(struct cas *cp)
343 for (i = 0; i < N_RX_COMP_RINGS; i++)
344 cas_enable_irq(cp, i);
347 static inline void cas_entropy_gather(struct cas *cp)
349 #ifdef USE_ENTROPY_DEV
350 if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
353 batch_entropy_store(readl(cp->regs + REG_ENTROPY_IV),
354 readl(cp->regs + REG_ENTROPY_IV),
359 static inline void cas_entropy_reset(struct cas *cp)
361 #ifdef USE_ENTROPY_DEV
362 if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
365 writel(BIM_LOCAL_DEV_PAD | BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_EXT,
366 cp->regs + REG_BIM_LOCAL_DEV_EN);
367 writeb(ENTROPY_RESET_STC_MODE, cp->regs + REG_ENTROPY_RESET);
368 writeb(0x55, cp->regs + REG_ENTROPY_RAND_REG);
370 /* if we read back 0x0, we don't have an entropy device */
371 if (readb(cp->regs + REG_ENTROPY_RAND_REG) == 0)
372 cp->cas_flags &= ~CAS_FLAG_ENTROPY_DEV;
376 /* access to the phy. the following assumes that we've initialized the MIF to
377 * be in frame rather than bit-bang mode
379 static u16 cas_phy_read(struct cas *cp, int reg)
382 int limit = STOP_TRIES_PHY;
384 cmd = MIF_FRAME_ST | MIF_FRAME_OP_READ;
385 cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
386 cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
387 cmd |= MIF_FRAME_TURN_AROUND_MSB;
388 writel(cmd, cp->regs + REG_MIF_FRAME);
390 /* poll for completion */
391 while (limit-- > 0) {
393 cmd = readl(cp->regs + REG_MIF_FRAME);
394 if (cmd & MIF_FRAME_TURN_AROUND_LSB)
395 return cmd & MIF_FRAME_DATA_MASK;
397 return 0xFFFF; /* -1 */
400 static int cas_phy_write(struct cas *cp, int reg, u16 val)
402 int limit = STOP_TRIES_PHY;
405 cmd = MIF_FRAME_ST | MIF_FRAME_OP_WRITE;
406 cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
407 cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
408 cmd |= MIF_FRAME_TURN_AROUND_MSB;
409 cmd |= val & MIF_FRAME_DATA_MASK;
410 writel(cmd, cp->regs + REG_MIF_FRAME);
412 /* poll for completion */
413 while (limit-- > 0) {
415 cmd = readl(cp->regs + REG_MIF_FRAME);
416 if (cmd & MIF_FRAME_TURN_AROUND_LSB)
422 static void cas_phy_powerup(struct cas *cp)
424 u16 ctl = cas_phy_read(cp, MII_BMCR);
426 if ((ctl & BMCR_PDOWN) == 0)
429 cas_phy_write(cp, MII_BMCR, ctl);
432 static void cas_phy_powerdown(struct cas *cp)
434 u16 ctl = cas_phy_read(cp, MII_BMCR);
436 if (ctl & BMCR_PDOWN)
439 cas_phy_write(cp, MII_BMCR, ctl);
442 /* cp->lock held. note: the last put_page will free the buffer */
443 static int cas_page_free(struct cas *cp, cas_page_t *page)
445 dma_unmap_page(&cp->pdev->dev, page->dma_addr, cp->page_size,
447 __free_pages(page->buffer, cp->page_order);
452 #ifdef RX_COUNT_BUFFERS
453 #define RX_USED_ADD(x, y) ((x)->used += (y))
454 #define RX_USED_SET(x, y) ((x)->used = (y))
456 #define RX_USED_ADD(x, y) do { } while(0)
457 #define RX_USED_SET(x, y) do { } while(0)
460 /* local page allocation routines for the receive buffers. jumbo pages
461 * require at least 8K contiguous and 8K aligned buffers.
463 static cas_page_t *cas_page_alloc(struct cas *cp, const gfp_t flags)
467 page = kmalloc(sizeof(cas_page_t), flags);
471 INIT_LIST_HEAD(&page->list);
472 RX_USED_SET(page, 0);
473 page->buffer = alloc_pages(flags, cp->page_order);
476 page->dma_addr = dma_map_page(&cp->pdev->dev, page->buffer, 0,
477 cp->page_size, DMA_FROM_DEVICE);
485 /* initialize spare pool of rx buffers, but allocate during the open */
486 static void cas_spare_init(struct cas *cp)
488 spin_lock(&cp->rx_inuse_lock);
489 INIT_LIST_HEAD(&cp->rx_inuse_list);
490 spin_unlock(&cp->rx_inuse_lock);
492 spin_lock(&cp->rx_spare_lock);
493 INIT_LIST_HEAD(&cp->rx_spare_list);
494 cp->rx_spares_needed = RX_SPARE_COUNT;
495 spin_unlock(&cp->rx_spare_lock);
498 /* used on close. free all the spare buffers. */
499 static void cas_spare_free(struct cas *cp)
501 struct list_head list, *elem, *tmp;
503 /* free spare buffers */
504 INIT_LIST_HEAD(&list);
505 spin_lock(&cp->rx_spare_lock);
506 list_splice_init(&cp->rx_spare_list, &list);
507 spin_unlock(&cp->rx_spare_lock);
508 list_for_each_safe(elem, tmp, &list) {
509 cas_page_free(cp, list_entry(elem, cas_page_t, list));
512 INIT_LIST_HEAD(&list);
515 * Looks like Adrian had protected this with a different
516 * lock than used everywhere else to manipulate this list.
518 spin_lock(&cp->rx_inuse_lock);
519 list_splice_init(&cp->rx_inuse_list, &list);
520 spin_unlock(&cp->rx_inuse_lock);
522 spin_lock(&cp->rx_spare_lock);
523 list_splice_init(&cp->rx_inuse_list, &list);
524 spin_unlock(&cp->rx_spare_lock);
526 list_for_each_safe(elem, tmp, &list) {
527 cas_page_free(cp, list_entry(elem, cas_page_t, list));
531 /* replenish spares if needed */
532 static void cas_spare_recover(struct cas *cp, const gfp_t flags)
534 struct list_head list, *elem, *tmp;
537 /* check inuse list. if we don't need any more free buffers,
541 /* make a local copy of the list */
542 INIT_LIST_HEAD(&list);
543 spin_lock(&cp->rx_inuse_lock);
544 list_splice_init(&cp->rx_inuse_list, &list);
545 spin_unlock(&cp->rx_inuse_lock);
547 list_for_each_safe(elem, tmp, &list) {
548 cas_page_t *page = list_entry(elem, cas_page_t, list);
551 * With the lockless pagecache, cassini buffering scheme gets
552 * slightly less accurate: we might find that a page has an
553 * elevated reference count here, due to a speculative ref,
554 * and skip it as in-use. Ideally we would be able to reclaim
555 * it. However this would be such a rare case, it doesn't
556 * matter too much as we should pick it up the next time round.
558 * Importantly, if we find that the page has a refcount of 1
559 * here (our refcount), then we know it is definitely not inuse
560 * so we can reuse it.
562 if (page_count(page->buffer) > 1)
566 spin_lock(&cp->rx_spare_lock);
567 if (cp->rx_spares_needed > 0) {
568 list_add(elem, &cp->rx_spare_list);
569 cp->rx_spares_needed--;
570 spin_unlock(&cp->rx_spare_lock);
572 spin_unlock(&cp->rx_spare_lock);
573 cas_page_free(cp, page);
577 /* put any inuse buffers back on the list */
578 if (!list_empty(&list)) {
579 spin_lock(&cp->rx_inuse_lock);
580 list_splice(&list, &cp->rx_inuse_list);
581 spin_unlock(&cp->rx_inuse_lock);
584 spin_lock(&cp->rx_spare_lock);
585 needed = cp->rx_spares_needed;
586 spin_unlock(&cp->rx_spare_lock);
590 /* we still need spares, so try to allocate some */
591 INIT_LIST_HEAD(&list);
594 cas_page_t *spare = cas_page_alloc(cp, flags);
597 list_add(&spare->list, &list);
601 spin_lock(&cp->rx_spare_lock);
602 list_splice(&list, &cp->rx_spare_list);
603 cp->rx_spares_needed -= i;
604 spin_unlock(&cp->rx_spare_lock);
607 /* pull a page from the list. */
608 static cas_page_t *cas_page_dequeue(struct cas *cp)
610 struct list_head *entry;
613 spin_lock(&cp->rx_spare_lock);
614 if (list_empty(&cp->rx_spare_list)) {
615 /* try to do a quick recovery */
616 spin_unlock(&cp->rx_spare_lock);
617 cas_spare_recover(cp, GFP_ATOMIC);
618 spin_lock(&cp->rx_spare_lock);
619 if (list_empty(&cp->rx_spare_list)) {
620 netif_err(cp, rx_err, cp->dev,
621 "no spare buffers available\n");
622 spin_unlock(&cp->rx_spare_lock);
627 entry = cp->rx_spare_list.next;
629 recover = ++cp->rx_spares_needed;
630 spin_unlock(&cp->rx_spare_lock);
632 /* trigger the timer to do the recovery */
633 if ((recover & (RX_SPARE_RECOVER_VAL - 1)) == 0) {
635 atomic_inc(&cp->reset_task_pending);
636 atomic_inc(&cp->reset_task_pending_spare);
637 schedule_work(&cp->reset_task);
639 atomic_set(&cp->reset_task_pending, CAS_RESET_SPARE);
640 schedule_work(&cp->reset_task);
643 return list_entry(entry, cas_page_t, list);
647 static void cas_mif_poll(struct cas *cp, const int enable)
651 cfg = readl(cp->regs + REG_MIF_CFG);
652 cfg &= (MIF_CFG_MDIO_0 | MIF_CFG_MDIO_1);
654 if (cp->phy_type & CAS_PHY_MII_MDIO1)
655 cfg |= MIF_CFG_PHY_SELECT;
657 /* poll and interrupt on link status change. */
659 cfg |= MIF_CFG_POLL_EN;
660 cfg |= CAS_BASE(MIF_CFG_POLL_REG, MII_BMSR);
661 cfg |= CAS_BASE(MIF_CFG_POLL_PHY, cp->phy_addr);
663 writel((enable) ? ~(BMSR_LSTATUS | BMSR_ANEGCOMPLETE) : 0xFFFF,
664 cp->regs + REG_MIF_MASK);
665 writel(cfg, cp->regs + REG_MIF_CFG);
668 /* Must be invoked under cp->lock */
669 static void cas_begin_auto_negotiation(struct cas *cp,
670 const struct ethtool_link_ksettings *ep)
676 int oldstate = cp->lstate;
677 int link_was_not_down = !(oldstate == link_down);
679 /* Setup link parameters */
682 lcntl = cp->link_cntl;
683 if (ep->base.autoneg == AUTONEG_ENABLE) {
684 cp->link_cntl = BMCR_ANENABLE;
686 u32 speed = ep->base.speed;
688 if (speed == SPEED_100)
689 cp->link_cntl |= BMCR_SPEED100;
690 else if (speed == SPEED_1000)
691 cp->link_cntl |= CAS_BMCR_SPEED1000;
692 if (ep->base.duplex == DUPLEX_FULL)
693 cp->link_cntl |= BMCR_FULLDPLX;
696 changed = (lcntl != cp->link_cntl);
699 if (cp->lstate == link_up) {
700 netdev_info(cp->dev, "PCS link down\n");
703 netdev_info(cp->dev, "link configuration changed\n");
706 cp->lstate = link_down;
707 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
712 * WTZ: If the old state was link_up, we turn off the carrier
713 * to replicate everything we do elsewhere on a link-down
714 * event when we were already in a link-up state..
716 if (oldstate == link_up)
717 netif_carrier_off(cp->dev);
718 if (changed && link_was_not_down) {
720 * WTZ: This branch will simply schedule a full reset after
721 * we explicitly changed link modes in an ioctl. See if this
722 * fixes the link-problems we were having for forced mode.
724 atomic_inc(&cp->reset_task_pending);
725 atomic_inc(&cp->reset_task_pending_all);
726 schedule_work(&cp->reset_task);
728 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
732 if (cp->phy_type & CAS_PHY_SERDES) {
733 u32 val = readl(cp->regs + REG_PCS_MII_CTRL);
735 if (cp->link_cntl & BMCR_ANENABLE) {
736 val |= (PCS_MII_RESTART_AUTONEG | PCS_MII_AUTONEG_EN);
737 cp->lstate = link_aneg;
739 if (cp->link_cntl & BMCR_FULLDPLX)
740 val |= PCS_MII_CTRL_DUPLEX;
741 val &= ~PCS_MII_AUTONEG_EN;
742 cp->lstate = link_force_ok;
744 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
745 writel(val, cp->regs + REG_PCS_MII_CTRL);
749 ctl = cas_phy_read(cp, MII_BMCR);
750 ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
751 CAS_BMCR_SPEED1000 | BMCR_ANENABLE);
752 ctl |= cp->link_cntl;
753 if (ctl & BMCR_ANENABLE) {
754 ctl |= BMCR_ANRESTART;
755 cp->lstate = link_aneg;
757 cp->lstate = link_force_ok;
759 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
760 cas_phy_write(cp, MII_BMCR, ctl);
765 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
768 /* Must be invoked under cp->lock. */
769 static int cas_reset_mii_phy(struct cas *cp)
771 int limit = STOP_TRIES_PHY;
774 cas_phy_write(cp, MII_BMCR, BMCR_RESET);
777 val = cas_phy_read(cp, MII_BMCR);
778 if ((val & BMCR_RESET) == 0)
785 static void cas_saturn_firmware_init(struct cas *cp)
787 const struct firmware *fw;
788 const char fw_name[] = "sun/cassini.bin";
791 if (PHY_NS_DP83065 != cp->phy_id)
794 err = request_firmware(&fw, fw_name, &cp->pdev->dev);
796 pr_err("Failed to load firmware \"%s\"\n",
801 pr_err("bogus length %zu in \"%s\"\n",
805 cp->fw_load_addr= fw->data[1] << 8 | fw->data[0];
806 cp->fw_size = fw->size - 2;
807 cp->fw_data = vmalloc(cp->fw_size);
810 memcpy(cp->fw_data, &fw->data[2], cp->fw_size);
812 release_firmware(fw);
815 static void cas_saturn_firmware_load(struct cas *cp)
822 cas_phy_powerdown(cp);
824 /* expanded memory access mode */
825 cas_phy_write(cp, DP83065_MII_MEM, 0x0);
827 /* pointer configuration for new firmware */
828 cas_phy_write(cp, DP83065_MII_REGE, 0x8ff9);
829 cas_phy_write(cp, DP83065_MII_REGD, 0xbd);
830 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffa);
831 cas_phy_write(cp, DP83065_MII_REGD, 0x82);
832 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffb);
833 cas_phy_write(cp, DP83065_MII_REGD, 0x0);
834 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffc);
835 cas_phy_write(cp, DP83065_MII_REGD, 0x39);
837 /* download new firmware */
838 cas_phy_write(cp, DP83065_MII_MEM, 0x1);
839 cas_phy_write(cp, DP83065_MII_REGE, cp->fw_load_addr);
840 for (i = 0; i < cp->fw_size; i++)
841 cas_phy_write(cp, DP83065_MII_REGD, cp->fw_data[i]);
843 /* enable firmware */
844 cas_phy_write(cp, DP83065_MII_REGE, 0x8ff8);
845 cas_phy_write(cp, DP83065_MII_REGD, 0x1);
849 /* phy initialization */
850 static void cas_phy_init(struct cas *cp)
854 /* if we're in MII/GMII mode, set up phy */
855 if (CAS_PHY_MII(cp->phy_type)) {
856 writel(PCS_DATAPATH_MODE_MII,
857 cp->regs + REG_PCS_DATAPATH_MODE);
860 cas_reset_mii_phy(cp); /* take out of isolate mode */
862 if (PHY_LUCENT_B0 == cp->phy_id) {
863 /* workaround link up/down issue with lucent */
864 cas_phy_write(cp, LUCENT_MII_REG, 0x8000);
865 cas_phy_write(cp, MII_BMCR, 0x00f1);
866 cas_phy_write(cp, LUCENT_MII_REG, 0x0);
868 } else if (PHY_BROADCOM_B0 == (cp->phy_id & 0xFFFFFFFC)) {
869 /* workarounds for broadcom phy */
870 cas_phy_write(cp, BROADCOM_MII_REG8, 0x0C20);
871 cas_phy_write(cp, BROADCOM_MII_REG7, 0x0012);
872 cas_phy_write(cp, BROADCOM_MII_REG5, 0x1804);
873 cas_phy_write(cp, BROADCOM_MII_REG7, 0x0013);
874 cas_phy_write(cp, BROADCOM_MII_REG5, 0x1204);
875 cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
876 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0132);
877 cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
878 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0232);
879 cas_phy_write(cp, BROADCOM_MII_REG7, 0x201F);
880 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0A20);
882 } else if (PHY_BROADCOM_5411 == cp->phy_id) {
883 val = cas_phy_read(cp, BROADCOM_MII_REG4);
884 val = cas_phy_read(cp, BROADCOM_MII_REG4);
886 /* link workaround */
887 cas_phy_write(cp, BROADCOM_MII_REG4,
891 } else if (cp->cas_flags & CAS_FLAG_SATURN) {
892 writel((cp->phy_type & CAS_PHY_MII_MDIO0) ?
893 SATURN_PCFG_FSI : 0x0,
894 cp->regs + REG_SATURN_PCFG);
896 /* load firmware to address 10Mbps auto-negotiation
897 * issue. NOTE: this will need to be changed if the
898 * default firmware gets fixed.
900 if (PHY_NS_DP83065 == cp->phy_id) {
901 cas_saturn_firmware_load(cp);
906 /* advertise capabilities */
907 val = cas_phy_read(cp, MII_BMCR);
908 val &= ~BMCR_ANENABLE;
909 cas_phy_write(cp, MII_BMCR, val);
912 cas_phy_write(cp, MII_ADVERTISE,
913 cas_phy_read(cp, MII_ADVERTISE) |
914 (ADVERTISE_10HALF | ADVERTISE_10FULL |
915 ADVERTISE_100HALF | ADVERTISE_100FULL |
916 CAS_ADVERTISE_PAUSE |
917 CAS_ADVERTISE_ASYM_PAUSE));
919 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
920 /* make sure that we don't advertise half
921 * duplex to avoid a chip issue
923 val = cas_phy_read(cp, CAS_MII_1000_CTRL);
924 val &= ~CAS_ADVERTISE_1000HALF;
925 val |= CAS_ADVERTISE_1000FULL;
926 cas_phy_write(cp, CAS_MII_1000_CTRL, val);
930 /* reset pcs for serdes */
934 writel(PCS_DATAPATH_MODE_SERDES,
935 cp->regs + REG_PCS_DATAPATH_MODE);
937 /* enable serdes pins on saturn */
938 if (cp->cas_flags & CAS_FLAG_SATURN)
939 writel(0, cp->regs + REG_SATURN_PCFG);
941 /* Reset PCS unit. */
942 val = readl(cp->regs + REG_PCS_MII_CTRL);
943 val |= PCS_MII_RESET;
944 writel(val, cp->regs + REG_PCS_MII_CTRL);
947 while (--limit > 0) {
949 if ((readl(cp->regs + REG_PCS_MII_CTRL) &
954 netdev_warn(cp->dev, "PCS reset bit would not clear [%08x]\n",
955 readl(cp->regs + REG_PCS_STATE_MACHINE));
957 /* Make sure PCS is disabled while changing advertisement
960 writel(0x0, cp->regs + REG_PCS_CFG);
962 /* Advertise all capabilities except half-duplex. */
963 val = readl(cp->regs + REG_PCS_MII_ADVERT);
964 val &= ~PCS_MII_ADVERT_HD;
965 val |= (PCS_MII_ADVERT_FD | PCS_MII_ADVERT_SYM_PAUSE |
966 PCS_MII_ADVERT_ASYM_PAUSE);
967 writel(val, cp->regs + REG_PCS_MII_ADVERT);
970 writel(PCS_CFG_EN, cp->regs + REG_PCS_CFG);
972 /* pcs workaround: enable sync detect */
973 writel(PCS_SERDES_CTRL_SYNCD_EN,
974 cp->regs + REG_PCS_SERDES_CTRL);
979 static int cas_pcs_link_check(struct cas *cp)
981 u32 stat, state_machine;
984 /* The link status bit latches on zero, so you must
985 * read it twice in such a case to see a transition
986 * to the link being up.
988 stat = readl(cp->regs + REG_PCS_MII_STATUS);
989 if ((stat & PCS_MII_STATUS_LINK_STATUS) == 0)
990 stat = readl(cp->regs + REG_PCS_MII_STATUS);
992 /* The remote-fault indication is only valid
993 * when autoneg has completed.
995 if ((stat & (PCS_MII_STATUS_AUTONEG_COMP |
996 PCS_MII_STATUS_REMOTE_FAULT)) ==
997 (PCS_MII_STATUS_AUTONEG_COMP | PCS_MII_STATUS_REMOTE_FAULT))
998 netif_info(cp, link, cp->dev, "PCS RemoteFault\n");
1000 /* work around link detection issue by querying the PCS state
1003 state_machine = readl(cp->regs + REG_PCS_STATE_MACHINE);
1004 if ((state_machine & PCS_SM_LINK_STATE_MASK) != SM_LINK_STATE_UP) {
1005 stat &= ~PCS_MII_STATUS_LINK_STATUS;
1006 } else if (state_machine & PCS_SM_WORD_SYNC_STATE_MASK) {
1007 stat |= PCS_MII_STATUS_LINK_STATUS;
1010 if (stat & PCS_MII_STATUS_LINK_STATUS) {
1011 if (cp->lstate != link_up) {
1013 cp->lstate = link_up;
1014 cp->link_transition = LINK_TRANSITION_LINK_UP;
1016 cas_set_link_modes(cp);
1017 netif_carrier_on(cp->dev);
1020 } else if (cp->lstate == link_up) {
1021 cp->lstate = link_down;
1022 if (link_transition_timeout != 0 &&
1023 cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1024 !cp->link_transition_jiffies_valid) {
1026 * force a reset, as a workaround for the
1027 * link-failure problem. May want to move this to a
1028 * point a bit earlier in the sequence. If we had
1029 * generated a reset a short time ago, we'll wait for
1030 * the link timer to check the status until a
1031 * timer expires (link_transistion_jiffies_valid is
1032 * true when the timer is running.) Instead of using
1033 * a system timer, we just do a check whenever the
1034 * link timer is running - this clears the flag after
1038 cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1039 cp->link_transition_jiffies = jiffies;
1040 cp->link_transition_jiffies_valid = 1;
1042 cp->link_transition = LINK_TRANSITION_ON_FAILURE;
1044 netif_carrier_off(cp->dev);
1046 netif_info(cp, link, cp->dev, "PCS link down\n");
1048 /* Cassini only: if you force a mode, there can be
1049 * sync problems on link down. to fix that, the following
1050 * things need to be checked:
1051 * 1) read serialink state register
1052 * 2) read pcs status register to verify link down.
1053 * 3) if link down and serial link == 0x03, then you need
1054 * to global reset the chip.
1056 if ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0) {
1057 /* should check to see if we're in a forced mode */
1058 stat = readl(cp->regs + REG_PCS_SERDES_STATE);
1062 } else if (cp->lstate == link_down) {
1063 if (link_transition_timeout != 0 &&
1064 cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1065 !cp->link_transition_jiffies_valid) {
1066 /* force a reset, as a workaround for the
1067 * link-failure problem. May want to move
1068 * this to a point a bit earlier in the
1072 cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1073 cp->link_transition_jiffies = jiffies;
1074 cp->link_transition_jiffies_valid = 1;
1076 cp->link_transition = LINK_TRANSITION_STILL_FAILED;
1083 static int cas_pcs_interrupt(struct net_device *dev,
1084 struct cas *cp, u32 status)
1086 u32 stat = readl(cp->regs + REG_PCS_INTR_STATUS);
1088 if ((stat & PCS_INTR_STATUS_LINK_CHANGE) == 0)
1090 return cas_pcs_link_check(cp);
1093 static int cas_txmac_interrupt(struct net_device *dev,
1094 struct cas *cp, u32 status)
1096 u32 txmac_stat = readl(cp->regs + REG_MAC_TX_STATUS);
1101 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1102 "txmac interrupt, txmac_stat: 0x%x\n", txmac_stat);
1104 /* Defer timer expiration is quite normal,
1105 * don't even log the event.
1107 if ((txmac_stat & MAC_TX_DEFER_TIMER) &&
1108 !(txmac_stat & ~MAC_TX_DEFER_TIMER))
1111 spin_lock(&cp->stat_lock[0]);
1112 if (txmac_stat & MAC_TX_UNDERRUN) {
1113 netdev_err(dev, "TX MAC xmit underrun\n");
1114 cp->net_stats[0].tx_fifo_errors++;
1117 if (txmac_stat & MAC_TX_MAX_PACKET_ERR) {
1118 netdev_err(dev, "TX MAC max packet size error\n");
1119 cp->net_stats[0].tx_errors++;
1122 /* The rest are all cases of one of the 16-bit TX
1123 * counters expiring.
1125 if (txmac_stat & MAC_TX_COLL_NORMAL)
1126 cp->net_stats[0].collisions += 0x10000;
1128 if (txmac_stat & MAC_TX_COLL_EXCESS) {
1129 cp->net_stats[0].tx_aborted_errors += 0x10000;
1130 cp->net_stats[0].collisions += 0x10000;
1133 if (txmac_stat & MAC_TX_COLL_LATE) {
1134 cp->net_stats[0].tx_aborted_errors += 0x10000;
1135 cp->net_stats[0].collisions += 0x10000;
1137 spin_unlock(&cp->stat_lock[0]);
1139 /* We do not keep track of MAC_TX_COLL_FIRST and
1140 * MAC_TX_PEAK_ATTEMPTS events.
1145 static void cas_load_firmware(struct cas *cp, cas_hp_inst_t *firmware)
1147 cas_hp_inst_t *inst;
1152 while ((inst = firmware) && inst->note) {
1153 writel(i, cp->regs + REG_HP_INSTR_RAM_ADDR);
1155 val = CAS_BASE(HP_INSTR_RAM_HI_VAL, inst->val);
1156 val |= CAS_BASE(HP_INSTR_RAM_HI_MASK, inst->mask);
1157 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_HI);
1159 val = CAS_BASE(HP_INSTR_RAM_MID_OUTARG, inst->outarg >> 10);
1160 val |= CAS_BASE(HP_INSTR_RAM_MID_OUTOP, inst->outop);
1161 val |= CAS_BASE(HP_INSTR_RAM_MID_FNEXT, inst->fnext);
1162 val |= CAS_BASE(HP_INSTR_RAM_MID_FOFF, inst->foff);
1163 val |= CAS_BASE(HP_INSTR_RAM_MID_SNEXT, inst->snext);
1164 val |= CAS_BASE(HP_INSTR_RAM_MID_SOFF, inst->soff);
1165 val |= CAS_BASE(HP_INSTR_RAM_MID_OP, inst->op);
1166 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_MID);
1168 val = CAS_BASE(HP_INSTR_RAM_LOW_OUTMASK, inst->outmask);
1169 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTSHIFT, inst->outshift);
1170 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTEN, inst->outenab);
1171 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTARG, inst->outarg);
1172 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_LOW);
1178 static void cas_init_rx_dma(struct cas *cp)
1180 u64 desc_dma = cp->block_dvma;
1184 /* rx free descriptors */
1185 val = CAS_BASE(RX_CFG_SWIVEL, RX_SWIVEL_OFF_VAL);
1186 val |= CAS_BASE(RX_CFG_DESC_RING, RX_DESC_RINGN_INDEX(0));
1187 val |= CAS_BASE(RX_CFG_COMP_RING, RX_COMP_RINGN_INDEX(0));
1188 if ((N_RX_DESC_RINGS > 1) &&
1189 (cp->cas_flags & CAS_FLAG_REG_PLUS)) /* do desc 2 */
1190 val |= CAS_BASE(RX_CFG_DESC_RING1, RX_DESC_RINGN_INDEX(1));
1191 writel(val, cp->regs + REG_RX_CFG);
1193 val = (unsigned long) cp->init_rxds[0] -
1194 (unsigned long) cp->init_block;
1195 writel((desc_dma + val) >> 32, cp->regs + REG_RX_DB_HI);
1196 writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_DB_LOW);
1197 writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
1199 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1200 /* rx desc 2 is for IPSEC packets. however,
1201 * we don't it that for that purpose.
1203 val = (unsigned long) cp->init_rxds[1] -
1204 (unsigned long) cp->init_block;
1205 writel((desc_dma + val) >> 32, cp->regs + REG_PLUS_RX_DB1_HI);
1206 writel((desc_dma + val) & 0xffffffff, cp->regs +
1207 REG_PLUS_RX_DB1_LOW);
1208 writel(RX_DESC_RINGN_SIZE(1) - 4, cp->regs +
1212 /* rx completion registers */
1213 val = (unsigned long) cp->init_rxcs[0] -
1214 (unsigned long) cp->init_block;
1215 writel((desc_dma + val) >> 32, cp->regs + REG_RX_CB_HI);
1216 writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_CB_LOW);
1218 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1220 for (i = 1; i < MAX_RX_COMP_RINGS; i++) {
1221 val = (unsigned long) cp->init_rxcs[i] -
1222 (unsigned long) cp->init_block;
1223 writel((desc_dma + val) >> 32, cp->regs +
1224 REG_PLUS_RX_CBN_HI(i));
1225 writel((desc_dma + val) & 0xffffffff, cp->regs +
1226 REG_PLUS_RX_CBN_LOW(i));
1230 /* read selective clear regs to prevent spurious interrupts
1231 * on reset because complete == kick.
1232 * selective clear set up to prevent interrupts on resets
1234 readl(cp->regs + REG_INTR_STATUS_ALIAS);
1235 writel(INTR_RX_DONE | INTR_RX_BUF_UNAVAIL, cp->regs + REG_ALIAS_CLEAR);
1237 /* set up pause thresholds */
1238 val = CAS_BASE(RX_PAUSE_THRESH_OFF,
1239 cp->rx_pause_off / RX_PAUSE_THRESH_QUANTUM);
1240 val |= CAS_BASE(RX_PAUSE_THRESH_ON,
1241 cp->rx_pause_on / RX_PAUSE_THRESH_QUANTUM);
1242 writel(val, cp->regs + REG_RX_PAUSE_THRESH);
1244 /* zero out dma reassembly buffers */
1245 for (i = 0; i < 64; i++) {
1246 writel(i, cp->regs + REG_RX_TABLE_ADDR);
1247 writel(0x0, cp->regs + REG_RX_TABLE_DATA_LOW);
1248 writel(0x0, cp->regs + REG_RX_TABLE_DATA_MID);
1249 writel(0x0, cp->regs + REG_RX_TABLE_DATA_HI);
1252 /* make sure address register is 0 for normal operation */
1253 writel(0x0, cp->regs + REG_RX_CTRL_FIFO_ADDR);
1254 writel(0x0, cp->regs + REG_RX_IPP_FIFO_ADDR);
1256 /* interrupt mitigation */
1258 val = CAS_BASE(RX_BLANK_INTR_TIME, RX_BLANK_INTR_TIME_VAL);
1259 val |= CAS_BASE(RX_BLANK_INTR_PKT, RX_BLANK_INTR_PKT_VAL);
1260 writel(val, cp->regs + REG_RX_BLANK);
1262 writel(0x0, cp->regs + REG_RX_BLANK);
1265 /* interrupt generation as a function of low water marks for
1266 * free desc and completion entries. these are used to trigger
1267 * housekeeping for rx descs. we don't use the free interrupt
1268 * as it's not very useful
1270 /* val = CAS_BASE(RX_AE_THRESH_FREE, RX_AE_FREEN_VAL(0)); */
1271 val = CAS_BASE(RX_AE_THRESH_COMP, RX_AE_COMP_VAL);
1272 writel(val, cp->regs + REG_RX_AE_THRESH);
1273 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1274 val = CAS_BASE(RX_AE1_THRESH_FREE, RX_AE_FREEN_VAL(1));
1275 writel(val, cp->regs + REG_PLUS_RX_AE1_THRESH);
1278 /* Random early detect registers. useful for congestion avoidance.
1279 * this should be tunable.
1281 writel(0x0, cp->regs + REG_RX_RED);
1283 /* receive page sizes. default == 2K (0x800) */
1285 if (cp->page_size == 0x1000)
1287 else if (cp->page_size == 0x2000)
1289 else if (cp->page_size == 0x4000)
1292 /* round mtu + offset. constrain to page size. */
1293 size = cp->dev->mtu + 64;
1294 if (size > cp->page_size)
1295 size = cp->page_size;
1299 else if (size <= 0x800)
1301 else if (size <= 0x1000)
1306 cp->mtu_stride = 1 << (i + 10);
1307 val = CAS_BASE(RX_PAGE_SIZE, val);
1308 val |= CAS_BASE(RX_PAGE_SIZE_MTU_STRIDE, i);
1309 val |= CAS_BASE(RX_PAGE_SIZE_MTU_COUNT, cp->page_size >> (i + 10));
1310 val |= CAS_BASE(RX_PAGE_SIZE_MTU_OFF, 0x1);
1311 writel(val, cp->regs + REG_RX_PAGE_SIZE);
1313 /* enable the header parser if desired */
1314 if (&CAS_HP_FIRMWARE[0] == &cas_prog_null[0])
1317 val = CAS_BASE(HP_CFG_NUM_CPU, CAS_NCPUS > 63 ? 0 : CAS_NCPUS);
1318 val |= HP_CFG_PARSE_EN | HP_CFG_SYN_INC_MASK;
1319 val |= CAS_BASE(HP_CFG_TCP_THRESH, HP_TCP_THRESH_VAL);
1320 writel(val, cp->regs + REG_HP_CFG);
1323 static inline void cas_rxc_init(struct cas_rx_comp *rxc)
1325 memset(rxc, 0, sizeof(*rxc));
1326 rxc->word4 = cpu_to_le64(RX_COMP4_ZERO);
1329 /* NOTE: we use the ENC RX DESC ring for spares. the rx_page[0,1]
1330 * flipping is protected by the fact that the chip will not
1331 * hand back the same page index while it's being processed.
1333 static inline cas_page_t *cas_page_spare(struct cas *cp, const int index)
1335 cas_page_t *page = cp->rx_pages[1][index];
1338 if (page_count(page->buffer) == 1)
1341 new = cas_page_dequeue(cp);
1343 spin_lock(&cp->rx_inuse_lock);
1344 list_add(&page->list, &cp->rx_inuse_list);
1345 spin_unlock(&cp->rx_inuse_lock);
1350 /* this needs to be changed if we actually use the ENC RX DESC ring */
1351 static cas_page_t *cas_page_swap(struct cas *cp, const int ring,
1354 cas_page_t **page0 = cp->rx_pages[0];
1355 cas_page_t **page1 = cp->rx_pages[1];
1357 /* swap if buffer is in use */
1358 if (page_count(page0[index]->buffer) > 1) {
1359 cas_page_t *new = cas_page_spare(cp, index);
1361 page1[index] = page0[index];
1365 RX_USED_SET(page0[index], 0);
1366 return page0[index];
1369 static void cas_clean_rxds(struct cas *cp)
1371 /* only clean ring 0 as ring 1 is used for spare buffers */
1372 struct cas_rx_desc *rxd = cp->init_rxds[0];
1375 /* release all rx flows */
1376 for (i = 0; i < N_RX_FLOWS; i++) {
1377 struct sk_buff *skb;
1378 while ((skb = __skb_dequeue(&cp->rx_flows[i]))) {
1379 cas_skb_release(skb);
1383 /* initialize descriptors */
1384 size = RX_DESC_RINGN_SIZE(0);
1385 for (i = 0; i < size; i++) {
1386 cas_page_t *page = cas_page_swap(cp, 0, i);
1387 rxd[i].buffer = cpu_to_le64(page->dma_addr);
1388 rxd[i].index = cpu_to_le64(CAS_BASE(RX_INDEX_NUM, i) |
1389 CAS_BASE(RX_INDEX_RING, 0));
1392 cp->rx_old[0] = RX_DESC_RINGN_SIZE(0) - 4;
1394 cp->cas_flags &= ~CAS_FLAG_RXD_POST(0);
1397 static void cas_clean_rxcs(struct cas *cp)
1401 /* take ownership of rx comp descriptors */
1402 memset(cp->rx_cur, 0, sizeof(*cp->rx_cur)*N_RX_COMP_RINGS);
1403 memset(cp->rx_new, 0, sizeof(*cp->rx_new)*N_RX_COMP_RINGS);
1404 for (i = 0; i < N_RX_COMP_RINGS; i++) {
1405 struct cas_rx_comp *rxc = cp->init_rxcs[i];
1406 for (j = 0; j < RX_COMP_RINGN_SIZE(i); j++) {
1407 cas_rxc_init(rxc + j);
1413 /* When we get a RX fifo overflow, the RX unit is probably hung
1414 * so we do the following.
1416 * If any part of the reset goes wrong, we return 1 and that causes the
1417 * whole chip to be reset.
1419 static int cas_rxmac_reset(struct cas *cp)
1421 struct net_device *dev = cp->dev;
1425 /* First, reset MAC RX. */
1426 writel(cp->mac_rx_cfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1427 for (limit = 0; limit < STOP_TRIES; limit++) {
1428 if (!(readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN))
1432 if (limit == STOP_TRIES) {
1433 netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
1437 /* Second, disable RX DMA. */
1438 writel(0, cp->regs + REG_RX_CFG);
1439 for (limit = 0; limit < STOP_TRIES; limit++) {
1440 if (!(readl(cp->regs + REG_RX_CFG) & RX_CFG_DMA_EN))
1444 if (limit == STOP_TRIES) {
1445 netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
1451 /* Execute RX reset command. */
1452 writel(SW_RESET_RX, cp->regs + REG_SW_RESET);
1453 for (limit = 0; limit < STOP_TRIES; limit++) {
1454 if (!(readl(cp->regs + REG_SW_RESET) & SW_RESET_RX))
1458 if (limit == STOP_TRIES) {
1459 netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
1463 /* reset driver rx state */
1467 /* Now, reprogram the rest of RX unit. */
1468 cas_init_rx_dma(cp);
1471 val = readl(cp->regs + REG_RX_CFG);
1472 writel(val | RX_CFG_DMA_EN, cp->regs + REG_RX_CFG);
1473 writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
1474 val = readl(cp->regs + REG_MAC_RX_CFG);
1475 writel(val | MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1480 static int cas_rxmac_interrupt(struct net_device *dev, struct cas *cp,
1483 u32 stat = readl(cp->regs + REG_MAC_RX_STATUS);
1488 netif_dbg(cp, intr, cp->dev, "rxmac interrupt, stat: 0x%x\n", stat);
1490 /* these are all rollovers */
1491 spin_lock(&cp->stat_lock[0]);
1492 if (stat & MAC_RX_ALIGN_ERR)
1493 cp->net_stats[0].rx_frame_errors += 0x10000;
1495 if (stat & MAC_RX_CRC_ERR)
1496 cp->net_stats[0].rx_crc_errors += 0x10000;
1498 if (stat & MAC_RX_LEN_ERR)
1499 cp->net_stats[0].rx_length_errors += 0x10000;
1501 if (stat & MAC_RX_OVERFLOW) {
1502 cp->net_stats[0].rx_over_errors++;
1503 cp->net_stats[0].rx_fifo_errors++;
1506 /* We do not track MAC_RX_FRAME_COUNT and MAC_RX_VIOL_ERR
1509 spin_unlock(&cp->stat_lock[0]);
1513 static int cas_mac_interrupt(struct net_device *dev, struct cas *cp,
1516 u32 stat = readl(cp->regs + REG_MAC_CTRL_STATUS);
1521 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1522 "mac interrupt, stat: 0x%x\n", stat);
1524 /* This interrupt is just for pause frame and pause
1525 * tracking. It is useful for diagnostics and debug
1526 * but probably by default we will mask these events.
1528 if (stat & MAC_CTRL_PAUSE_STATE)
1529 cp->pause_entered++;
1531 if (stat & MAC_CTRL_PAUSE_RECEIVED)
1532 cp->pause_last_time_recvd = (stat >> 16);
1538 /* Must be invoked under cp->lock. */
1539 static inline int cas_mdio_link_not_up(struct cas *cp)
1543 switch (cp->lstate) {
1544 case link_force_ret:
1545 netif_info(cp, link, cp->dev, "Autoneg failed again, keeping forced mode\n");
1546 cas_phy_write(cp, MII_BMCR, cp->link_fcntl);
1547 cp->timer_ticks = 5;
1548 cp->lstate = link_force_ok;
1549 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1553 val = cas_phy_read(cp, MII_BMCR);
1555 /* Try forced modes. we try things in the following order:
1556 * 1000 full -> 100 full/half -> 10 half
1558 val &= ~(BMCR_ANRESTART | BMCR_ANENABLE);
1559 val |= BMCR_FULLDPLX;
1560 val |= (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
1561 CAS_BMCR_SPEED1000 : BMCR_SPEED100;
1562 cas_phy_write(cp, MII_BMCR, val);
1563 cp->timer_ticks = 5;
1564 cp->lstate = link_force_try;
1565 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1568 case link_force_try:
1569 /* Downgrade from 1000 to 100 to 10 Mbps if necessary. */
1570 val = cas_phy_read(cp, MII_BMCR);
1571 cp->timer_ticks = 5;
1572 if (val & CAS_BMCR_SPEED1000) { /* gigabit */
1573 val &= ~CAS_BMCR_SPEED1000;
1574 val |= (BMCR_SPEED100 | BMCR_FULLDPLX);
1575 cas_phy_write(cp, MII_BMCR, val);
1579 if (val & BMCR_SPEED100) {
1580 if (val & BMCR_FULLDPLX) /* fd failed */
1581 val &= ~BMCR_FULLDPLX;
1582 else { /* 100Mbps failed */
1583 val &= ~BMCR_SPEED100;
1585 cas_phy_write(cp, MII_BMCR, val);
1596 /* must be invoked with cp->lock held */
1597 static int cas_mii_link_check(struct cas *cp, const u16 bmsr)
1601 if (bmsr & BMSR_LSTATUS) {
1602 /* Ok, here we got a link. If we had it due to a forced
1603 * fallback, and we were configured for autoneg, we
1604 * retry a short autoneg pass. If you know your hub is
1605 * broken, use ethtool ;)
1607 if ((cp->lstate == link_force_try) &&
1608 (cp->link_cntl & BMCR_ANENABLE)) {
1609 cp->lstate = link_force_ret;
1610 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1611 cas_mif_poll(cp, 0);
1612 cp->link_fcntl = cas_phy_read(cp, MII_BMCR);
1613 cp->timer_ticks = 5;
1615 netif_info(cp, link, cp->dev,
1616 "Got link after fallback, retrying autoneg once...\n");
1617 cas_phy_write(cp, MII_BMCR,
1618 cp->link_fcntl | BMCR_ANENABLE |
1620 cas_mif_poll(cp, 1);
1622 } else if (cp->lstate != link_up) {
1623 cp->lstate = link_up;
1624 cp->link_transition = LINK_TRANSITION_LINK_UP;
1627 cas_set_link_modes(cp);
1628 netif_carrier_on(cp->dev);
1634 /* link not up. if the link was previously up, we restart the
1638 if (cp->lstate == link_up) {
1639 cp->lstate = link_down;
1640 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
1642 netif_carrier_off(cp->dev);
1644 netif_info(cp, link, cp->dev, "Link down\n");
1647 } else if (++cp->timer_ticks > 10)
1648 cas_mdio_link_not_up(cp);
1653 static int cas_mif_interrupt(struct net_device *dev, struct cas *cp,
1656 u32 stat = readl(cp->regs + REG_MIF_STATUS);
1659 /* check for a link change */
1660 if (CAS_VAL(MIF_STATUS_POLL_STATUS, stat) == 0)
1663 bmsr = CAS_VAL(MIF_STATUS_POLL_DATA, stat);
1664 return cas_mii_link_check(cp, bmsr);
1667 static int cas_pci_interrupt(struct net_device *dev, struct cas *cp,
1670 u32 stat = readl(cp->regs + REG_PCI_ERR_STATUS);
1675 netdev_err(dev, "PCI error [%04x:%04x]",
1676 stat, readl(cp->regs + REG_BIM_DIAG));
1678 /* cassini+ has this reserved */
1679 if ((stat & PCI_ERR_BADACK) &&
1680 ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0))
1681 pr_cont(" <No ACK64# during ABS64 cycle>");
1683 if (stat & PCI_ERR_DTRTO)
1684 pr_cont(" <Delayed transaction timeout>");
1685 if (stat & PCI_ERR_OTHER)
1686 pr_cont(" <other>");
1687 if (stat & PCI_ERR_BIM_DMA_WRITE)
1688 pr_cont(" <BIM DMA 0 write req>");
1689 if (stat & PCI_ERR_BIM_DMA_READ)
1690 pr_cont(" <BIM DMA 0 read req>");
1693 if (stat & PCI_ERR_OTHER) {
1696 /* Interrogate PCI config space for the
1699 pci_errs = pci_status_get_and_clear_errors(cp->pdev);
1701 netdev_err(dev, "PCI status errors[%04x]\n", pci_errs);
1702 if (pci_errs & PCI_STATUS_PARITY)
1703 netdev_err(dev, "PCI parity error detected\n");
1704 if (pci_errs & PCI_STATUS_SIG_TARGET_ABORT)
1705 netdev_err(dev, "PCI target abort\n");
1706 if (pci_errs & PCI_STATUS_REC_TARGET_ABORT)
1707 netdev_err(dev, "PCI master acks target abort\n");
1708 if (pci_errs & PCI_STATUS_REC_MASTER_ABORT)
1709 netdev_err(dev, "PCI master abort\n");
1710 if (pci_errs & PCI_STATUS_SIG_SYSTEM_ERROR)
1711 netdev_err(dev, "PCI system error SERR#\n");
1712 if (pci_errs & PCI_STATUS_DETECTED_PARITY)
1713 netdev_err(dev, "PCI parity error\n");
1716 /* For all PCI errors, we should reset the chip. */
1720 /* All non-normal interrupt conditions get serviced here.
1721 * Returns non-zero if we should just exit the interrupt
1722 * handler right now (ie. if we reset the card which invalidates
1723 * all of the other original irq status bits).
1725 static int cas_abnormal_irq(struct net_device *dev, struct cas *cp,
1728 if (status & INTR_RX_TAG_ERROR) {
1729 /* corrupt RX tag framing */
1730 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1731 "corrupt rx tag framing\n");
1732 spin_lock(&cp->stat_lock[0]);
1733 cp->net_stats[0].rx_errors++;
1734 spin_unlock(&cp->stat_lock[0]);
1738 if (status & INTR_RX_LEN_MISMATCH) {
1739 /* length mismatch. */
1740 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1741 "length mismatch for rx frame\n");
1742 spin_lock(&cp->stat_lock[0]);
1743 cp->net_stats[0].rx_errors++;
1744 spin_unlock(&cp->stat_lock[0]);
1748 if (status & INTR_PCS_STATUS) {
1749 if (cas_pcs_interrupt(dev, cp, status))
1753 if (status & INTR_TX_MAC_STATUS) {
1754 if (cas_txmac_interrupt(dev, cp, status))
1758 if (status & INTR_RX_MAC_STATUS) {
1759 if (cas_rxmac_interrupt(dev, cp, status))
1763 if (status & INTR_MAC_CTRL_STATUS) {
1764 if (cas_mac_interrupt(dev, cp, status))
1768 if (status & INTR_MIF_STATUS) {
1769 if (cas_mif_interrupt(dev, cp, status))
1773 if (status & INTR_PCI_ERROR_STATUS) {
1774 if (cas_pci_interrupt(dev, cp, status))
1781 atomic_inc(&cp->reset_task_pending);
1782 atomic_inc(&cp->reset_task_pending_all);
1783 netdev_err(dev, "reset called in cas_abnormal_irq [0x%x]\n", status);
1784 schedule_work(&cp->reset_task);
1786 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
1787 netdev_err(dev, "reset called in cas_abnormal_irq\n");
1788 schedule_work(&cp->reset_task);
1793 /* NOTE: CAS_TABORT returns 1 or 2 so that it can be used when
1794 * determining whether to do a netif_stop/wakeup
1796 #define CAS_TABORT(x) (((x)->cas_flags & CAS_FLAG_TARGET_ABORT) ? 2 : 1)
1797 #define CAS_ROUND_PAGE(x) (((x) + PAGE_SIZE - 1) & PAGE_MASK)
1798 static inline int cas_calc_tabort(struct cas *cp, const unsigned long addr,
1801 unsigned long off = addr + len;
1803 if (CAS_TABORT(cp) == 1)
1805 if ((CAS_ROUND_PAGE(off) - off) > TX_TARGET_ABORT_LEN)
1807 return TX_TARGET_ABORT_LEN;
1810 static inline void cas_tx_ringN(struct cas *cp, int ring, int limit)
1812 struct cas_tx_desc *txds;
1813 struct sk_buff **skbs;
1814 struct net_device *dev = cp->dev;
1817 spin_lock(&cp->tx_lock[ring]);
1818 txds = cp->init_txds[ring];
1819 skbs = cp->tx_skbs[ring];
1820 entry = cp->tx_old[ring];
1822 count = TX_BUFF_COUNT(ring, entry, limit);
1823 while (entry != limit) {
1824 struct sk_buff *skb = skbs[entry];
1830 /* this should never occur */
1831 entry = TX_DESC_NEXT(ring, entry);
1835 /* however, we might get only a partial skb release. */
1836 count -= skb_shinfo(skb)->nr_frags +
1837 + cp->tx_tiny_use[ring][entry].nbufs + 1;
1841 netif_printk(cp, tx_done, KERN_DEBUG, cp->dev,
1842 "tx[%d] done, slot %d\n", ring, entry);
1845 cp->tx_tiny_use[ring][entry].nbufs = 0;
1847 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1848 struct cas_tx_desc *txd = txds + entry;
1850 daddr = le64_to_cpu(txd->buffer);
1851 dlen = CAS_VAL(TX_DESC_BUFLEN,
1852 le64_to_cpu(txd->control));
1853 dma_unmap_page(&cp->pdev->dev, daddr, dlen,
1855 entry = TX_DESC_NEXT(ring, entry);
1857 /* tiny buffer may follow */
1858 if (cp->tx_tiny_use[ring][entry].used) {
1859 cp->tx_tiny_use[ring][entry].used = 0;
1860 entry = TX_DESC_NEXT(ring, entry);
1864 spin_lock(&cp->stat_lock[ring]);
1865 cp->net_stats[ring].tx_packets++;
1866 cp->net_stats[ring].tx_bytes += skb->len;
1867 spin_unlock(&cp->stat_lock[ring]);
1868 dev_consume_skb_irq(skb);
1870 cp->tx_old[ring] = entry;
1872 /* this is wrong for multiple tx rings. the net device needs
1873 * multiple queues for this to do the right thing. we wait
1874 * for 2*packets to be available when using tiny buffers
1876 if (netif_queue_stopped(dev) &&
1877 (TX_BUFFS_AVAIL(cp, ring) > CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1)))
1878 netif_wake_queue(dev);
1879 spin_unlock(&cp->tx_lock[ring]);
1882 static void cas_tx(struct net_device *dev, struct cas *cp,
1886 #ifdef USE_TX_COMPWB
1887 u64 compwb = le64_to_cpu(cp->init_block->tx_compwb);
1889 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1890 "tx interrupt, status: 0x%x, %llx\n",
1891 status, (unsigned long long)compwb);
1892 /* process all the rings */
1893 for (ring = 0; ring < N_TX_RINGS; ring++) {
1894 #ifdef USE_TX_COMPWB
1895 /* use the completion writeback registers */
1896 limit = (CAS_VAL(TX_COMPWB_MSB, compwb) << 8) |
1897 CAS_VAL(TX_COMPWB_LSB, compwb);
1898 compwb = TX_COMPWB_NEXT(compwb);
1900 limit = readl(cp->regs + REG_TX_COMPN(ring));
1902 if (cp->tx_old[ring] != limit)
1903 cas_tx_ringN(cp, ring, limit);
1908 static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
1909 int entry, const u64 *words,
1910 struct sk_buff **skbref)
1912 int dlen, hlen, len, i, alloclen;
1913 int off, swivel = RX_SWIVEL_OFF_VAL;
1914 struct cas_page *page;
1915 struct sk_buff *skb;
1920 hlen = CAS_VAL(RX_COMP2_HDR_SIZE, words[1]);
1921 dlen = CAS_VAL(RX_COMP1_DATA_SIZE, words[0]);
1924 if (RX_COPY_ALWAYS || (words[2] & RX_COMP3_SMALL_PKT))
1927 alloclen = max(hlen, RX_COPY_MIN);
1929 skb = netdev_alloc_skb(cp->dev, alloclen + swivel + cp->crc_size);
1934 skb_reserve(skb, swivel);
1938 if (hlen) { /* always copy header pages */
1939 i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
1940 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
1941 off = CAS_VAL(RX_COMP2_HDR_OFF, words[1]) * 0x100 +
1945 if (!dlen) /* attach FCS */
1947 dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
1948 i, DMA_FROM_DEVICE);
1949 memcpy(p, page_address(page->buffer) + off, i);
1950 dma_sync_single_for_device(&cp->pdev->dev,
1951 page->dma_addr + off, i,
1953 RX_USED_ADD(page, 0x100);
1959 if (alloclen < (hlen + dlen)) {
1960 skb_frag_t *frag = skb_shinfo(skb)->frags;
1962 /* normal or jumbo packets. we use frags */
1963 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
1964 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
1965 off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
1967 hlen = min(cp->page_size - off, dlen);
1969 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1970 "rx page overflow: %d\n", hlen);
1971 dev_kfree_skb_irq(skb);
1975 if (i == dlen) /* attach FCS */
1977 dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
1978 i, DMA_FROM_DEVICE);
1980 /* make sure we always copy a header */
1982 if (p == (char *) skb->data) { /* not split */
1983 memcpy(p, page_address(page->buffer) + off,
1985 dma_sync_single_for_device(&cp->pdev->dev,
1986 page->dma_addr + off, i,
1989 swivel = RX_COPY_MIN;
1990 RX_USED_ADD(page, cp->mtu_stride);
1992 RX_USED_ADD(page, hlen);
1994 skb_put(skb, alloclen);
1996 skb_shinfo(skb)->nr_frags++;
1997 skb->data_len += hlen - swivel;
1998 skb->truesize += hlen - swivel;
1999 skb->len += hlen - swivel;
2001 __skb_frag_set_page(frag, page->buffer);
2002 __skb_frag_ref(frag);
2003 skb_frag_off_set(frag, off);
2004 skb_frag_size_set(frag, hlen - swivel);
2006 /* any more data? */
2007 if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2011 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2012 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2013 dma_sync_single_for_cpu(&cp->pdev->dev,
2015 hlen + cp->crc_size,
2017 dma_sync_single_for_device(&cp->pdev->dev,
2019 hlen + cp->crc_size,
2022 skb_shinfo(skb)->nr_frags++;
2023 skb->data_len += hlen;
2027 __skb_frag_set_page(frag, page->buffer);
2028 __skb_frag_ref(frag);
2029 skb_frag_off_set(frag, 0);
2030 skb_frag_size_set(frag, hlen);
2031 RX_USED_ADD(page, hlen + cp->crc_size);
2035 crcaddr = page_address(page->buffer) + off + hlen;
2038 /* copying packet */
2042 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2043 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2044 off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
2045 hlen = min(cp->page_size - off, dlen);
2047 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
2048 "rx page overflow: %d\n", hlen);
2049 dev_kfree_skb_irq(skb);
2053 if (i == dlen) /* attach FCS */
2055 dma_sync_single_for_cpu(&cp->pdev->dev, page->dma_addr + off,
2056 i, DMA_FROM_DEVICE);
2057 memcpy(p, page_address(page->buffer) + off, i);
2058 dma_sync_single_for_device(&cp->pdev->dev,
2059 page->dma_addr + off, i,
2061 if (p == (char *) skb->data) /* not split */
2062 RX_USED_ADD(page, cp->mtu_stride);
2064 RX_USED_ADD(page, i);
2066 /* any more data? */
2067 if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2069 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2070 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2071 dma_sync_single_for_cpu(&cp->pdev->dev,
2073 dlen + cp->crc_size,
2075 memcpy(p, page_address(page->buffer), dlen + cp->crc_size);
2076 dma_sync_single_for_device(&cp->pdev->dev,
2078 dlen + cp->crc_size,
2080 RX_USED_ADD(page, dlen + cp->crc_size);
2084 crcaddr = skb->data + alloclen;
2086 skb_put(skb, alloclen);
2089 csum = (__force __sum16)htons(CAS_VAL(RX_COMP4_TCP_CSUM, words[3]));
2091 /* checksum includes FCS. strip it out. */
2092 csum = csum_fold(csum_partial(crcaddr, cp->crc_size,
2093 csum_unfold(csum)));
2095 skb->protocol = eth_type_trans(skb, cp->dev);
2096 if (skb->protocol == htons(ETH_P_IP)) {
2097 skb->csum = csum_unfold(~csum);
2098 skb->ip_summed = CHECKSUM_COMPLETE;
2100 skb_checksum_none_assert(skb);
2105 /* we can handle up to 64 rx flows at a time. we do the same thing
2106 * as nonreassm except that we batch up the buffers.
2107 * NOTE: we currently just treat each flow as a bunch of packets that
2108 * we pass up. a better way would be to coalesce the packets
2109 * into a jumbo packet. to do that, we need to do the following:
2110 * 1) the first packet will have a clean split between header and
2112 * 2) each time the next flow packet comes in, extend the
2113 * data length and merge the checksums.
2114 * 3) on flow release, fix up the header.
2115 * 4) make sure the higher layer doesn't care.
2116 * because packets get coalesced, we shouldn't run into fragment count
2119 static inline void cas_rx_flow_pkt(struct cas *cp, const u64 *words,
2120 struct sk_buff *skb)
2122 int flowid = CAS_VAL(RX_COMP3_FLOWID, words[2]) & (N_RX_FLOWS - 1);
2123 struct sk_buff_head *flow = &cp->rx_flows[flowid];
2125 /* this is protected at a higher layer, so no need to
2126 * do any additional locking here. stick the buffer
2129 __skb_queue_tail(flow, skb);
2130 if (words[0] & RX_COMP1_RELEASE_FLOW) {
2131 while ((skb = __skb_dequeue(flow))) {
2132 cas_skb_release(skb);
2137 /* put rx descriptor back on ring. if a buffer is in use by a higher
2138 * layer, this will need to put in a replacement.
2140 static void cas_post_page(struct cas *cp, const int ring, const int index)
2145 entry = cp->rx_old[ring];
2147 new = cas_page_swap(cp, ring, index);
2148 cp->init_rxds[ring][entry].buffer = cpu_to_le64(new->dma_addr);
2149 cp->init_rxds[ring][entry].index =
2150 cpu_to_le64(CAS_BASE(RX_INDEX_NUM, index) |
2151 CAS_BASE(RX_INDEX_RING, ring));
2153 entry = RX_DESC_ENTRY(ring, entry + 1);
2154 cp->rx_old[ring] = entry;
2160 writel(entry, cp->regs + REG_RX_KICK);
2161 else if ((N_RX_DESC_RINGS > 1) &&
2162 (cp->cas_flags & CAS_FLAG_REG_PLUS))
2163 writel(entry, cp->regs + REG_PLUS_RX_KICK1);
2167 /* only when things are bad */
2168 static int cas_post_rxds_ringN(struct cas *cp, int ring, int num)
2170 unsigned int entry, last, count, released;
2172 cas_page_t **page = cp->rx_pages[ring];
2174 entry = cp->rx_old[ring];
2176 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2177 "rxd[%d] interrupt, done: %d\n", ring, entry);
2180 count = entry & 0x3;
2181 last = RX_DESC_ENTRY(ring, num ? entry + num - 4: entry - 4);
2183 while (entry != last) {
2184 /* make a new buffer if it's still in use */
2185 if (page_count(page[entry]->buffer) > 1) {
2186 cas_page_t *new = cas_page_dequeue(cp);
2188 /* let the timer know that we need to
2191 cp->cas_flags |= CAS_FLAG_RXD_POST(ring);
2192 if (!timer_pending(&cp->link_timer))
2193 mod_timer(&cp->link_timer, jiffies +
2194 CAS_LINK_FAST_TIMEOUT);
2195 cp->rx_old[ring] = entry;
2196 cp->rx_last[ring] = num ? num - released : 0;
2199 spin_lock(&cp->rx_inuse_lock);
2200 list_add(&page[entry]->list, &cp->rx_inuse_list);
2201 spin_unlock(&cp->rx_inuse_lock);
2202 cp->init_rxds[ring][entry].buffer =
2203 cpu_to_le64(new->dma_addr);
2213 entry = RX_DESC_ENTRY(ring, entry + 1);
2215 cp->rx_old[ring] = entry;
2221 writel(cluster, cp->regs + REG_RX_KICK);
2222 else if ((N_RX_DESC_RINGS > 1) &&
2223 (cp->cas_flags & CAS_FLAG_REG_PLUS))
2224 writel(cluster, cp->regs + REG_PLUS_RX_KICK1);
2229 /* process a completion ring. packets are set up in three basic ways:
2230 * small packets: should be copied header + data in single buffer.
2231 * large packets: header and data in a single buffer.
2232 * split packets: header in a separate buffer from data.
2233 * data may be in multiple pages. data may be > 256
2234 * bytes but in a single page.
2236 * NOTE: RX page posting is done in this routine as well. while there's
2237 * the capability of using multiple RX completion rings, it isn't
2238 * really worthwhile due to the fact that the page posting will
2239 * force serialization on the single descriptor ring.
2241 static int cas_rx_ringN(struct cas *cp, int ring, int budget)
2243 struct cas_rx_comp *rxcs = cp->init_rxcs[ring];
2247 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2248 "rx[%d] interrupt, done: %d/%d\n",
2250 readl(cp->regs + REG_RX_COMP_HEAD), cp->rx_new[ring]);
2252 entry = cp->rx_new[ring];
2255 struct cas_rx_comp *rxc = rxcs + entry;
2256 struct sk_buff *skb;
2261 words[0] = le64_to_cpu(rxc->word1);
2262 words[1] = le64_to_cpu(rxc->word2);
2263 words[2] = le64_to_cpu(rxc->word3);
2264 words[3] = le64_to_cpu(rxc->word4);
2266 /* don't touch if still owned by hw */
2267 type = CAS_VAL(RX_COMP1_TYPE, words[0]);
2271 /* hw hasn't cleared the zero bit yet */
2272 if (words[3] & RX_COMP4_ZERO) {
2276 /* get info on the packet */
2277 if (words[3] & (RX_COMP4_LEN_MISMATCH | RX_COMP4_BAD)) {
2278 spin_lock(&cp->stat_lock[ring]);
2279 cp->net_stats[ring].rx_errors++;
2280 if (words[3] & RX_COMP4_LEN_MISMATCH)
2281 cp->net_stats[ring].rx_length_errors++;
2282 if (words[3] & RX_COMP4_BAD)
2283 cp->net_stats[ring].rx_crc_errors++;
2284 spin_unlock(&cp->stat_lock[ring]);
2286 /* We'll just return it to Cassini. */
2288 spin_lock(&cp->stat_lock[ring]);
2289 ++cp->net_stats[ring].rx_dropped;
2290 spin_unlock(&cp->stat_lock[ring]);
2294 len = cas_rx_process_pkt(cp, rxc, entry, words, &skb);
2300 /* see if it's a flow re-assembly or not. the driver
2301 * itself handles release back up.
2303 if (RX_DONT_BATCH || (type == 0x2)) {
2304 /* non-reassm: these always get released */
2305 cas_skb_release(skb);
2307 cas_rx_flow_pkt(cp, words, skb);
2310 spin_lock(&cp->stat_lock[ring]);
2311 cp->net_stats[ring].rx_packets++;
2312 cp->net_stats[ring].rx_bytes += len;
2313 spin_unlock(&cp->stat_lock[ring]);
2318 /* should it be released? */
2319 if (words[0] & RX_COMP1_RELEASE_HDR) {
2320 i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
2321 dring = CAS_VAL(RX_INDEX_RING, i);
2322 i = CAS_VAL(RX_INDEX_NUM, i);
2323 cas_post_page(cp, dring, i);
2326 if (words[0] & RX_COMP1_RELEASE_DATA) {
2327 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2328 dring = CAS_VAL(RX_INDEX_RING, i);
2329 i = CAS_VAL(RX_INDEX_NUM, i);
2330 cas_post_page(cp, dring, i);
2333 if (words[0] & RX_COMP1_RELEASE_NEXT) {
2334 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2335 dring = CAS_VAL(RX_INDEX_RING, i);
2336 i = CAS_VAL(RX_INDEX_NUM, i);
2337 cas_post_page(cp, dring, i);
2340 /* skip to the next entry */
2341 entry = RX_COMP_ENTRY(ring, entry + 1 +
2342 CAS_VAL(RX_COMP1_SKIP, words[0]));
2344 if (budget && (npackets >= budget))
2348 cp->rx_new[ring] = entry;
2351 netdev_info(cp->dev, "Memory squeeze, deferring packet\n");
2356 /* put completion entries back on the ring */
2357 static void cas_post_rxcs_ringN(struct net_device *dev,
2358 struct cas *cp, int ring)
2360 struct cas_rx_comp *rxc = cp->init_rxcs[ring];
2363 last = cp->rx_cur[ring];
2364 entry = cp->rx_new[ring];
2365 netif_printk(cp, intr, KERN_DEBUG, dev,
2366 "rxc[%d] interrupt, done: %d/%d\n",
2367 ring, readl(cp->regs + REG_RX_COMP_HEAD), entry);
2369 /* zero and re-mark descriptors */
2370 while (last != entry) {
2371 cas_rxc_init(rxc + last);
2372 last = RX_COMP_ENTRY(ring, last + 1);
2374 cp->rx_cur[ring] = last;
2377 writel(last, cp->regs + REG_RX_COMP_TAIL);
2378 else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
2379 writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
2384 /* cassini can use all four PCI interrupts for the completion ring.
2385 * rings 3 and 4 are identical
2387 #if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
2388 static inline void cas_handle_irqN(struct net_device *dev,
2389 struct cas *cp, const u32 status,
2392 if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
2393 cas_post_rxcs_ringN(dev, cp, ring);
2396 static irqreturn_t cas_interruptN(int irq, void *dev_id)
2398 struct net_device *dev = dev_id;
2399 struct cas *cp = netdev_priv(dev);
2400 unsigned long flags;
2401 int ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
2402 u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
2404 /* check for shared irq */
2408 spin_lock_irqsave(&cp->lock, flags);
2409 if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2412 napi_schedule(&cp->napi);
2414 cas_rx_ringN(cp, ring, 0);
2416 status &= ~INTR_RX_DONE_ALT;
2420 cas_handle_irqN(dev, cp, status, ring);
2421 spin_unlock_irqrestore(&cp->lock, flags);
2427 /* everything but rx packets */
2428 static inline void cas_handle_irq1(struct cas *cp, const u32 status)
2430 if (status & INTR_RX_BUF_UNAVAIL_1) {
2431 /* Frame arrived, no free RX buffers available.
2432 * NOTE: we can get this on a link transition. */
2433 cas_post_rxds_ringN(cp, 1, 0);
2434 spin_lock(&cp->stat_lock[1]);
2435 cp->net_stats[1].rx_dropped++;
2436 spin_unlock(&cp->stat_lock[1]);
2439 if (status & INTR_RX_BUF_AE_1)
2440 cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
2441 RX_AE_FREEN_VAL(1));
2443 if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2444 cas_post_rxcs_ringN(cp, 1);
2447 /* ring 2 handles a few more events than 3 and 4 */
2448 static irqreturn_t cas_interrupt1(int irq, void *dev_id)
2450 struct net_device *dev = dev_id;
2451 struct cas *cp = netdev_priv(dev);
2452 unsigned long flags;
2453 u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2455 /* check for shared interrupt */
2459 spin_lock_irqsave(&cp->lock, flags);
2460 if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2463 napi_schedule(&cp->napi);
2465 cas_rx_ringN(cp, 1, 0);
2467 status &= ~INTR_RX_DONE_ALT;
2470 cas_handle_irq1(cp, status);
2471 spin_unlock_irqrestore(&cp->lock, flags);
2476 static inline void cas_handle_irq(struct net_device *dev,
2477 struct cas *cp, const u32 status)
2479 /* housekeeping interrupts */
2480 if (status & INTR_ERROR_MASK)
2481 cas_abnormal_irq(dev, cp, status);
2483 if (status & INTR_RX_BUF_UNAVAIL) {
2484 /* Frame arrived, no free RX buffers available.
2485 * NOTE: we can get this on a link transition.
2487 cas_post_rxds_ringN(cp, 0, 0);
2488 spin_lock(&cp->stat_lock[0]);
2489 cp->net_stats[0].rx_dropped++;
2490 spin_unlock(&cp->stat_lock[0]);
2491 } else if (status & INTR_RX_BUF_AE) {
2492 cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
2493 RX_AE_FREEN_VAL(0));
2496 if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2497 cas_post_rxcs_ringN(dev, cp, 0);
2500 static irqreturn_t cas_interrupt(int irq, void *dev_id)
2502 struct net_device *dev = dev_id;
2503 struct cas *cp = netdev_priv(dev);
2504 unsigned long flags;
2505 u32 status = readl(cp->regs + REG_INTR_STATUS);
2510 spin_lock_irqsave(&cp->lock, flags);
2511 if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
2512 cas_tx(dev, cp, status);
2513 status &= ~(INTR_TX_ALL | INTR_TX_INTME);
2516 if (status & INTR_RX_DONE) {
2519 napi_schedule(&cp->napi);
2521 cas_rx_ringN(cp, 0, 0);
2523 status &= ~INTR_RX_DONE;
2527 cas_handle_irq(dev, cp, status);
2528 spin_unlock_irqrestore(&cp->lock, flags);
2534 static int cas_poll(struct napi_struct *napi, int budget)
2536 struct cas *cp = container_of(napi, struct cas, napi);
2537 struct net_device *dev = cp->dev;
2538 int i, enable_intr, credits;
2539 u32 status = readl(cp->regs + REG_INTR_STATUS);
2540 unsigned long flags;
2542 spin_lock_irqsave(&cp->lock, flags);
2543 cas_tx(dev, cp, status);
2544 spin_unlock_irqrestore(&cp->lock, flags);
2546 /* NAPI rx packets. we spread the credits across all of the
2549 * to make sure we're fair with the work we loop through each
2550 * ring N_RX_COMP_RING times with a request of
2551 * budget / N_RX_COMP_RINGS
2555 for (i = 0; i < N_RX_COMP_RINGS; i++) {
2557 for (j = 0; j < N_RX_COMP_RINGS; j++) {
2558 credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
2559 if (credits >= budget) {
2567 /* final rx completion */
2568 spin_lock_irqsave(&cp->lock, flags);
2570 cas_handle_irq(dev, cp, status);
2573 if (N_RX_COMP_RINGS > 1) {
2574 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2576 cas_handle_irq1(dev, cp, status);
2581 if (N_RX_COMP_RINGS > 2) {
2582 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
2584 cas_handle_irqN(dev, cp, status, 2);
2589 if (N_RX_COMP_RINGS > 3) {
2590 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
2592 cas_handle_irqN(dev, cp, status, 3);
2595 spin_unlock_irqrestore(&cp->lock, flags);
2597 napi_complete(napi);
2598 cas_unmask_intr(cp);
2604 #ifdef CONFIG_NET_POLL_CONTROLLER
2605 static void cas_netpoll(struct net_device *dev)
2607 struct cas *cp = netdev_priv(dev);
2609 cas_disable_irq(cp, 0);
2610 cas_interrupt(cp->pdev->irq, dev);
2611 cas_enable_irq(cp, 0);
2614 if (N_RX_COMP_RINGS > 1) {
2615 /* cas_interrupt1(); */
2619 if (N_RX_COMP_RINGS > 2) {
2620 /* cas_interruptN(); */
2624 if (N_RX_COMP_RINGS > 3) {
2625 /* cas_interruptN(); */
2631 static void cas_tx_timeout(struct net_device *dev, unsigned int txqueue)
2633 struct cas *cp = netdev_priv(dev);
2635 netdev_err(dev, "transmit timed out, resetting\n");
2636 if (!cp->hw_running) {
2637 netdev_err(dev, "hrm.. hw not running!\n");
2641 netdev_err(dev, "MIF_STATE[%08x]\n",
2642 readl(cp->regs + REG_MIF_STATE_MACHINE));
2644 netdev_err(dev, "MAC_STATE[%08x]\n",
2645 readl(cp->regs + REG_MAC_STATE_MACHINE));
2647 netdev_err(dev, "TX_STATE[%08x:%08x:%08x] FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
2648 readl(cp->regs + REG_TX_CFG),
2649 readl(cp->regs + REG_MAC_TX_STATUS),
2650 readl(cp->regs + REG_MAC_TX_CFG),
2651 readl(cp->regs + REG_TX_FIFO_PKT_CNT),
2652 readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
2653 readl(cp->regs + REG_TX_FIFO_READ_PTR),
2654 readl(cp->regs + REG_TX_SM_1),
2655 readl(cp->regs + REG_TX_SM_2));
2657 netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
2658 readl(cp->regs + REG_RX_CFG),
2659 readl(cp->regs + REG_MAC_RX_STATUS),
2660 readl(cp->regs + REG_MAC_RX_CFG));
2662 netdev_err(dev, "HP_STATE[%08x:%08x:%08x:%08x]\n",
2663 readl(cp->regs + REG_HP_STATE_MACHINE),
2664 readl(cp->regs + REG_HP_STATUS0),
2665 readl(cp->regs + REG_HP_STATUS1),
2666 readl(cp->regs + REG_HP_STATUS2));
2669 atomic_inc(&cp->reset_task_pending);
2670 atomic_inc(&cp->reset_task_pending_all);
2671 schedule_work(&cp->reset_task);
2673 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
2674 schedule_work(&cp->reset_task);
2678 static inline int cas_intme(int ring, int entry)
2680 /* Algorithm: IRQ every 1/2 of descriptors. */
2681 if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
2687 static void cas_write_txd(struct cas *cp, int ring, int entry,
2688 dma_addr_t mapping, int len, u64 ctrl, int last)
2690 struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
2692 ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
2693 if (cas_intme(ring, entry))
2694 ctrl |= TX_DESC_INTME;
2696 ctrl |= TX_DESC_EOF;
2697 txd->control = cpu_to_le64(ctrl);
2698 txd->buffer = cpu_to_le64(mapping);
2701 static inline void *tx_tiny_buf(struct cas *cp, const int ring,
2704 return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
2707 static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
2708 const int entry, const int tentry)
2710 cp->tx_tiny_use[ring][tentry].nbufs++;
2711 cp->tx_tiny_use[ring][entry].used = 1;
2712 return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
2715 static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
2716 struct sk_buff *skb)
2718 struct net_device *dev = cp->dev;
2719 int entry, nr_frags, frag, tabort, tentry;
2721 unsigned long flags;
2725 spin_lock_irqsave(&cp->tx_lock[ring], flags);
2727 /* This is a hard error, log it. */
2728 if (TX_BUFFS_AVAIL(cp, ring) <=
2729 CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
2730 netif_stop_queue(dev);
2731 spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
2732 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
2737 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2738 const u64 csum_start_off = skb_checksum_start_offset(skb);
2739 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
2741 ctrl = TX_DESC_CSUM_EN |
2742 CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
2743 CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
2746 entry = cp->tx_new[ring];
2747 cp->tx_skbs[ring][entry] = skb;
2749 nr_frags = skb_shinfo(skb)->nr_frags;
2750 len = skb_headlen(skb);
2751 mapping = dma_map_page(&cp->pdev->dev, virt_to_page(skb->data),
2752 offset_in_page(skb->data), len, DMA_TO_DEVICE);
2755 tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
2756 if (unlikely(tabort)) {
2757 /* NOTE: len is always > tabort */
2758 cas_write_txd(cp, ring, entry, mapping, len - tabort,
2759 ctrl | TX_DESC_SOF, 0);
2760 entry = TX_DESC_NEXT(ring, entry);
2762 skb_copy_from_linear_data_offset(skb, len - tabort,
2763 tx_tiny_buf(cp, ring, entry), tabort);
2764 mapping = tx_tiny_map(cp, ring, entry, tentry);
2765 cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
2768 cas_write_txd(cp, ring, entry, mapping, len, ctrl |
2769 TX_DESC_SOF, (nr_frags == 0));
2771 entry = TX_DESC_NEXT(ring, entry);
2773 for (frag = 0; frag < nr_frags; frag++) {
2774 const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
2776 len = skb_frag_size(fragp);
2777 mapping = skb_frag_dma_map(&cp->pdev->dev, fragp, 0, len,
2780 tabort = cas_calc_tabort(cp, skb_frag_off(fragp), len);
2781 if (unlikely(tabort)) {
2782 /* NOTE: len is always > tabort */
2783 cas_write_txd(cp, ring, entry, mapping, len - tabort,
2785 entry = TX_DESC_NEXT(ring, entry);
2786 memcpy_from_page(tx_tiny_buf(cp, ring, entry),
2787 skb_frag_page(fragp),
2788 skb_frag_off(fragp) + len - tabort,
2790 mapping = tx_tiny_map(cp, ring, entry, tentry);
2794 cas_write_txd(cp, ring, entry, mapping, len, ctrl,
2795 (frag + 1 == nr_frags));
2796 entry = TX_DESC_NEXT(ring, entry);
2799 cp->tx_new[ring] = entry;
2800 if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
2801 netif_stop_queue(dev);
2803 netif_printk(cp, tx_queued, KERN_DEBUG, dev,
2804 "tx[%d] queued, slot %d, skblen %d, avail %d\n",
2805 ring, entry, skb->len, TX_BUFFS_AVAIL(cp, ring));
2806 writel(entry, cp->regs + REG_TX_KICKN(ring));
2807 spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
2811 static netdev_tx_t cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
2813 struct cas *cp = netdev_priv(dev);
2815 /* this is only used as a load-balancing hint, so it doesn't
2816 * need to be SMP safe
2820 if (skb_padto(skb, cp->min_frame_size))
2821 return NETDEV_TX_OK;
2823 /* XXX: we need some higher-level QoS hooks to steer packets to
2824 * individual queues.
2826 if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
2827 return NETDEV_TX_BUSY;
2828 return NETDEV_TX_OK;
2831 static void cas_init_tx_dma(struct cas *cp)
2833 u64 desc_dma = cp->block_dvma;
2838 /* set up tx completion writeback registers. must be 8-byte aligned */
2839 #ifdef USE_TX_COMPWB
2840 off = offsetof(struct cas_init_block, tx_compwb);
2841 writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
2842 writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
2845 /* enable completion writebacks, enable paced mode,
2846 * disable read pipe, and disable pre-interrupt compwbs
2848 val = TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
2849 TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
2850 TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
2851 TX_CFG_INTR_COMPWB_DIS;
2853 /* write out tx ring info and tx desc bases */
2854 for (i = 0; i < MAX_TX_RINGS; i++) {
2855 off = (unsigned long) cp->init_txds[i] -
2856 (unsigned long) cp->init_block;
2858 val |= CAS_TX_RINGN_BASE(i);
2859 writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
2860 writel((desc_dma + off) & 0xffffffff, cp->regs +
2862 /* don't zero out the kick register here as the system
2866 writel(val, cp->regs + REG_TX_CFG);
2868 /* program max burst sizes. these numbers should be different
2872 writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2873 writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
2874 writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
2875 writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
2877 writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2878 writel(0x800, cp->regs + REG_TX_MAXBURST_1);
2879 writel(0x800, cp->regs + REG_TX_MAXBURST_2);
2880 writel(0x800, cp->regs + REG_TX_MAXBURST_3);
2884 /* Must be invoked under cp->lock. */
2885 static inline void cas_init_dma(struct cas *cp)
2887 cas_init_tx_dma(cp);
2888 cas_init_rx_dma(cp);
2891 static void cas_process_mc_list(struct cas *cp)
2895 struct netdev_hw_addr *ha;
2898 memset(hash_table, 0, sizeof(hash_table));
2899 netdev_for_each_mc_addr(ha, cp->dev) {
2900 if (i <= CAS_MC_EXACT_MATCH_SIZE) {
2901 /* use the alternate mac address registers for the
2902 * first 15 multicast addresses
2904 writel((ha->addr[4] << 8) | ha->addr[5],
2905 cp->regs + REG_MAC_ADDRN(i*3 + 0));
2906 writel((ha->addr[2] << 8) | ha->addr[3],
2907 cp->regs + REG_MAC_ADDRN(i*3 + 1));
2908 writel((ha->addr[0] << 8) | ha->addr[1],
2909 cp->regs + REG_MAC_ADDRN(i*3 + 2));
2913 /* use hw hash table for the next series of
2914 * multicast addresses
2916 crc = ether_crc_le(ETH_ALEN, ha->addr);
2918 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
2921 for (i = 0; i < 16; i++)
2922 writel(hash_table[i], cp->regs + REG_MAC_HASH_TABLEN(i));
2925 /* Must be invoked under cp->lock. */
2926 static u32 cas_setup_multicast(struct cas *cp)
2931 if (cp->dev->flags & IFF_PROMISC) {
2932 rxcfg |= MAC_RX_CFG_PROMISC_EN;
2934 } else if (cp->dev->flags & IFF_ALLMULTI) {
2935 for (i=0; i < 16; i++)
2936 writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
2937 rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2940 cas_process_mc_list(cp);
2941 rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2947 /* must be invoked under cp->stat_lock[N_TX_RINGS] */
2948 static void cas_clear_mac_err(struct cas *cp)
2950 writel(0, cp->regs + REG_MAC_COLL_NORMAL);
2951 writel(0, cp->regs + REG_MAC_COLL_FIRST);
2952 writel(0, cp->regs + REG_MAC_COLL_EXCESS);
2953 writel(0, cp->regs + REG_MAC_COLL_LATE);
2954 writel(0, cp->regs + REG_MAC_TIMER_DEFER);
2955 writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
2956 writel(0, cp->regs + REG_MAC_RECV_FRAME);
2957 writel(0, cp->regs + REG_MAC_LEN_ERR);
2958 writel(0, cp->regs + REG_MAC_ALIGN_ERR);
2959 writel(0, cp->regs + REG_MAC_FCS_ERR);
2960 writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
2964 static void cas_mac_reset(struct cas *cp)
2968 /* do both TX and RX reset */
2969 writel(0x1, cp->regs + REG_MAC_TX_RESET);
2970 writel(0x1, cp->regs + REG_MAC_RX_RESET);
2975 if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
2983 if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
2988 if (readl(cp->regs + REG_MAC_TX_RESET) |
2989 readl(cp->regs + REG_MAC_RX_RESET))
2990 netdev_err(cp->dev, "mac tx[%d]/rx[%d] reset failed [%08x]\n",
2991 readl(cp->regs + REG_MAC_TX_RESET),
2992 readl(cp->regs + REG_MAC_RX_RESET),
2993 readl(cp->regs + REG_MAC_STATE_MACHINE));
2997 /* Must be invoked under cp->lock. */
2998 static void cas_init_mac(struct cas *cp)
3000 const unsigned char *e = &cp->dev->dev_addr[0];
3004 /* setup core arbitration weight register */
3005 writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
3007 #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
3008 /* set the infinite burst register for chips that don't have
3011 if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
3012 writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
3015 writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
3017 writel(0x00, cp->regs + REG_MAC_IPG0);
3018 writel(0x08, cp->regs + REG_MAC_IPG1);
3019 writel(0x04, cp->regs + REG_MAC_IPG2);
3021 /* change later for 802.3z */
3022 writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
3024 /* min frame + FCS */
3025 writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
3027 /* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
3028 * specify the maximum frame size to prevent RX tag errors on
3031 writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
3032 CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
3033 (CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
3034 cp->regs + REG_MAC_FRAMESIZE_MAX);
3036 /* NOTE: crc_size is used as a surrogate for half-duplex.
3037 * workaround saturn half-duplex issue by increasing preamble
3040 if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
3041 writel(0x41, cp->regs + REG_MAC_PA_SIZE);
3043 writel(0x07, cp->regs + REG_MAC_PA_SIZE);
3044 writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
3045 writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
3046 writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
3048 writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
3050 writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
3051 writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
3052 writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
3053 writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
3054 writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
3056 /* setup mac address in perfect filter array */
3057 for (i = 0; i < 45; i++)
3058 writel(0x0, cp->regs + REG_MAC_ADDRN(i));
3060 writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
3061 writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
3062 writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
3064 writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
3065 writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
3066 writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
3068 cp->mac_rx_cfg = cas_setup_multicast(cp);
3070 spin_lock(&cp->stat_lock[N_TX_RINGS]);
3071 cas_clear_mac_err(cp);
3072 spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3074 /* Setup MAC interrupts. We want to get all of the interesting
3075 * counter expiration events, but we do not want to hear about
3076 * normal rx/tx as the DMA engine tells us that.
3078 writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
3079 writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
3081 /* Don't enable even the PAUSE interrupts for now, we
3082 * make no use of those events other than to record them.
3084 writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
3087 /* Must be invoked under cp->lock. */
3088 static void cas_init_pause_thresholds(struct cas *cp)
3090 /* Calculate pause thresholds. Setting the OFF threshold to the
3091 * full RX fifo size effectively disables PAUSE generation
3093 if (cp->rx_fifo_size <= (2 * 1024)) {
3094 cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
3096 int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
3097 if (max_frame * 3 > cp->rx_fifo_size) {
3098 cp->rx_pause_off = 7104;
3099 cp->rx_pause_on = 960;
3101 int off = (cp->rx_fifo_size - (max_frame * 2));
3102 int on = off - max_frame;
3103 cp->rx_pause_off = off;
3104 cp->rx_pause_on = on;
3109 static int cas_vpd_match(const void __iomem *p, const char *str)
3111 int len = strlen(str) + 1;
3114 for (i = 0; i < len; i++) {
3115 if (readb(p + i) != str[i])
3122 /* get the mac address by reading the vpd information in the rom.
3123 * also get the phy type and determine if there's an entropy generator.
3124 * NOTE: this is a bit convoluted for the following reasons:
3125 * 1) vpd info has order-dependent mac addresses for multinic cards
3126 * 2) the only way to determine the nic order is to use the slot
3128 * 3) fiber cards don't have bridges, so their slot numbers don't
3130 * 4) we don't actually know we have a fiber card until after
3131 * the mac addresses are parsed.
3133 static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
3136 void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
3137 void __iomem *base, *kstart;
3140 #define VPD_FOUND_MAC 0x01
3141 #define VPD_FOUND_PHY 0x02
3143 int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
3146 #if defined(CONFIG_SPARC)
3147 const unsigned char *addr;
3150 /* give us access to the PROM */
3151 writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
3152 cp->regs + REG_BIM_LOCAL_DEV_EN);
3154 /* check for an expansion rom */
3155 if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
3156 goto use_random_mac_addr;
3158 /* search for beginning of vpd */
3160 for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
3161 /* check for PCIR */
3162 if ((readb(p + i + 0) == 0x50) &&
3163 (readb(p + i + 1) == 0x43) &&
3164 (readb(p + i + 2) == 0x49) &&
3165 (readb(p + i + 3) == 0x52)) {
3166 base = p + (readb(p + i + 8) |
3167 (readb(p + i + 9) << 8));
3172 if (!base || (readb(base) != 0x82))
3173 goto use_random_mac_addr;
3175 i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
3176 while (i < EXPANSION_ROM_SIZE) {
3177 if (readb(base + i) != 0x90) /* no vpd found */
3178 goto use_random_mac_addr;
3180 /* found a vpd field */
3181 len = readb(base + i + 1) | (readb(base + i + 2) << 8);
3183 /* extract keywords */
3184 kstart = base + i + 3;
3186 while ((p - kstart) < len) {
3187 int klen = readb(p + 2);
3193 /* look for the following things:
3194 * -- correct length == 29
3195 * 3 (type) + 2 (size) +
3196 * 18 (strlen("local-mac-address") + 1) +
3198 * -- VPD Instance 'I'
3199 * -- VPD Type Bytes 'B'
3200 * -- VPD data length == 6
3201 * -- property string == local-mac-address
3203 * -- correct length == 24
3204 * 3 (type) + 2 (size) +
3205 * 12 (strlen("entropy-dev") + 1) +
3206 * 7 (strlen("vms110") + 1)
3207 * -- VPD Instance 'I'
3208 * -- VPD Type String 'B'
3209 * -- VPD data length == 7
3210 * -- property string == entropy-dev
3212 * -- correct length == 18
3213 * 3 (type) + 2 (size) +
3214 * 9 (strlen("phy-type") + 1) +
3215 * 4 (strlen("pcs") + 1)
3216 * -- VPD Instance 'I'
3217 * -- VPD Type String 'S'
3218 * -- VPD data length == 4
3219 * -- property string == phy-type
3221 * -- correct length == 23
3222 * 3 (type) + 2 (size) +
3223 * 14 (strlen("phy-interface") + 1) +
3224 * 4 (strlen("pcs") + 1)
3225 * -- VPD Instance 'I'
3226 * -- VPD Type String 'S'
3227 * -- VPD data length == 4
3228 * -- property string == phy-interface
3230 if (readb(p) != 'I')
3233 /* finally, check string and length */
3234 type = readb(p + 3);
3236 if ((klen == 29) && readb(p + 4) == 6 &&
3237 cas_vpd_match(p + 5,
3238 "local-mac-address")) {
3239 if (mac_off++ > offset)
3242 /* set mac address */
3243 for (j = 0; j < 6; j++)
3253 #ifdef USE_ENTROPY_DEV
3255 cas_vpd_match(p + 5, "entropy-dev") &&
3256 cas_vpd_match(p + 17, "vms110")) {
3257 cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
3262 if (found & VPD_FOUND_PHY)
3265 if ((klen == 18) && readb(p + 4) == 4 &&
3266 cas_vpd_match(p + 5, "phy-type")) {
3267 if (cas_vpd_match(p + 14, "pcs")) {
3268 phy_type = CAS_PHY_SERDES;
3273 if ((klen == 23) && readb(p + 4) == 4 &&
3274 cas_vpd_match(p + 5, "phy-interface")) {
3275 if (cas_vpd_match(p + 19, "pcs")) {
3276 phy_type = CAS_PHY_SERDES;
3281 found |= VPD_FOUND_MAC;
3285 found |= VPD_FOUND_PHY;
3293 use_random_mac_addr:
3294 if (found & VPD_FOUND_MAC)
3297 #if defined(CONFIG_SPARC)
3298 addr = of_get_property(cp->of_node, "local-mac-address", NULL);
3300 memcpy(dev_addr, addr, ETH_ALEN);
3305 /* Sun MAC prefix then 3 random bytes. */
3306 pr_info("MAC address not found in ROM VPD\n");
3310 get_random_bytes(dev_addr + 3, 3);
3313 writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
3317 /* check pci invariants */
3318 static void cas_check_pci_invariants(struct cas *cp)
3320 struct pci_dev *pdev = cp->pdev;
3323 if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
3324 (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
3325 if (pdev->revision >= CAS_ID_REVPLUS)
3326 cp->cas_flags |= CAS_FLAG_REG_PLUS;
3327 if (pdev->revision < CAS_ID_REVPLUS02u)
3328 cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
3330 /* Original Cassini supports HW CSUM, but it's not
3331 * enabled by default as it can trigger TX hangs.
3333 if (pdev->revision < CAS_ID_REV2)
3334 cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
3336 /* Only sun has original cassini chips. */
3337 cp->cas_flags |= CAS_FLAG_REG_PLUS;
3339 /* We use a flag because the same phy might be externally
3342 if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
3343 (pdev->device == PCI_DEVICE_ID_NS_SATURN))
3344 cp->cas_flags |= CAS_FLAG_SATURN;
3349 static int cas_check_invariants(struct cas *cp)
3351 struct pci_dev *pdev = cp->pdev;
3356 /* get page size for rx buffers. */
3358 #ifdef USE_PAGE_ORDER
3359 if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
3360 /* see if we can allocate larger pages */
3361 struct page *page = alloc_pages(GFP_ATOMIC,
3362 CAS_JUMBO_PAGE_SHIFT -
3365 __free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
3366 cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
3368 printk("MTU limited to %d bytes\n", CAS_MAX_MTU);
3372 cp->page_size = (PAGE_SIZE << cp->page_order);
3374 /* Fetch the FIFO configurations. */
3375 cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
3376 cp->rx_fifo_size = RX_FIFO_SIZE;
3378 /* finish phy determination. MDIO1 takes precedence over MDIO0 if
3379 * they're both connected.
3381 cp->phy_type = cas_get_vpd_info(cp, addr, PCI_SLOT(pdev->devfn));
3382 eth_hw_addr_set(cp->dev, addr);
3383 if (cp->phy_type & CAS_PHY_SERDES) {
3384 cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3385 return 0; /* no more checking needed */
3389 cfg = readl(cp->regs + REG_MIF_CFG);
3390 if (cfg & MIF_CFG_MDIO_1) {
3391 cp->phy_type = CAS_PHY_MII_MDIO1;
3392 } else if (cfg & MIF_CFG_MDIO_0) {
3393 cp->phy_type = CAS_PHY_MII_MDIO0;
3396 cas_mif_poll(cp, 0);
3397 writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3399 for (i = 0; i < 32; i++) {
3403 for (j = 0; j < 3; j++) {
3405 phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
3406 phy_id |= cas_phy_read(cp, MII_PHYSID2);
3407 if (phy_id && (phy_id != 0xFFFFFFFF)) {
3408 cp->phy_id = phy_id;
3413 pr_err("MII phy did not respond [%08x]\n",
3414 readl(cp->regs + REG_MIF_STATE_MACHINE));
3418 /* see if we can do gigabit */
3419 cfg = cas_phy_read(cp, MII_BMSR);
3420 if ((cfg & CAS_BMSR_1000_EXTEND) &&
3421 cas_phy_read(cp, CAS_MII_1000_EXTEND))
3422 cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3426 /* Must be invoked under cp->lock. */
3427 static inline void cas_start_dma(struct cas *cp)
3434 val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
3435 writel(val, cp->regs + REG_TX_CFG);
3436 val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
3437 writel(val, cp->regs + REG_RX_CFG);
3439 /* enable the mac */
3440 val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
3441 writel(val, cp->regs + REG_MAC_TX_CFG);
3442 val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
3443 writel(val, cp->regs + REG_MAC_RX_CFG);
3447 val = readl(cp->regs + REG_MAC_TX_CFG);
3448 if ((val & MAC_TX_CFG_EN))
3452 if (i < 0) txfailed = 1;
3455 val = readl(cp->regs + REG_MAC_RX_CFG);
3456 if ((val & MAC_RX_CFG_EN)) {
3459 "enabling mac failed [tx:%08x:%08x]\n",
3460 readl(cp->regs + REG_MIF_STATE_MACHINE),
3461 readl(cp->regs + REG_MAC_STATE_MACHINE));
3463 goto enable_rx_done;
3467 netdev_err(cp->dev, "enabling mac failed [%s:%08x:%08x]\n",
3468 (txfailed ? "tx,rx" : "rx"),
3469 readl(cp->regs + REG_MIF_STATE_MACHINE),
3470 readl(cp->regs + REG_MAC_STATE_MACHINE));
3473 cas_unmask_intr(cp); /* enable interrupts */
3474 writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
3475 writel(0, cp->regs + REG_RX_COMP_TAIL);
3477 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
3478 if (N_RX_DESC_RINGS > 1)
3479 writel(RX_DESC_RINGN_SIZE(1) - 4,
3480 cp->regs + REG_PLUS_RX_KICK1);
3484 /* Must be invoked under cp->lock. */
3485 static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
3488 u32 val = readl(cp->regs + REG_PCS_MII_LPA);
3489 *fd = (val & PCS_MII_LPA_FD) ? 1 : 0;
3490 *pause = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
3491 if (val & PCS_MII_LPA_ASYM_PAUSE)
3496 /* Must be invoked under cp->lock. */
3497 static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
3506 /* use GMII registers */
3507 val = cas_phy_read(cp, MII_LPA);
3508 if (val & CAS_LPA_PAUSE)
3511 if (val & CAS_LPA_ASYM_PAUSE)
3514 if (val & LPA_DUPLEX)
3519 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
3520 val = cas_phy_read(cp, CAS_MII_1000_STATUS);
3521 if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
3523 if (val & CAS_LPA_1000FULL)
3528 /* A link-up condition has occurred, initialize and enable the
3531 * Must be invoked under cp->lock.
3533 static void cas_set_link_modes(struct cas *cp)
3536 int full_duplex, speed, pause;
3542 if (CAS_PHY_MII(cp->phy_type)) {
3543 cas_mif_poll(cp, 0);
3544 val = cas_phy_read(cp, MII_BMCR);
3545 if (val & BMCR_ANENABLE) {
3546 cas_read_mii_link_mode(cp, &full_duplex, &speed,
3549 if (val & BMCR_FULLDPLX)
3552 if (val & BMCR_SPEED100)
3554 else if (val & CAS_BMCR_SPEED1000)
3555 speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
3558 cas_mif_poll(cp, 1);
3561 val = readl(cp->regs + REG_PCS_MII_CTRL);
3562 cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
3563 if ((val & PCS_MII_AUTONEG_EN) == 0) {
3564 if (val & PCS_MII_CTRL_DUPLEX)
3569 netif_info(cp, link, cp->dev, "Link up at %d Mbps, %s-duplex\n",
3570 speed, full_duplex ? "full" : "half");
3572 val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
3573 if (CAS_PHY_MII(cp->phy_type)) {
3574 val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
3576 val |= MAC_XIF_DISABLE_ECHO;
3579 val |= MAC_XIF_FDPLX_LED;
3581 val |= MAC_XIF_GMII_MODE;
3582 writel(val, cp->regs + REG_MAC_XIF_CFG);
3584 /* deal with carrier and collision detect. */
3585 val = MAC_TX_CFG_IPG_EN;
3587 val |= MAC_TX_CFG_IGNORE_CARRIER;
3588 val |= MAC_TX_CFG_IGNORE_COLL;
3590 #ifndef USE_CSMA_CD_PROTO
3591 val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
3592 val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
3595 /* val now set up for REG_MAC_TX_CFG */
3597 /* If gigabit and half-duplex, enable carrier extension
3598 * mode. increase slot time to 512 bytes as well.
3599 * else, disable it and make sure slot time is 64 bytes.
3600 * also activate checksum bug workaround
3602 if ((speed == 1000) && !full_duplex) {
3603 writel(val | MAC_TX_CFG_CARRIER_EXTEND,
3604 cp->regs + REG_MAC_TX_CFG);
3606 val = readl(cp->regs + REG_MAC_RX_CFG);
3607 val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
3608 writel(val | MAC_RX_CFG_CARRIER_EXTEND,
3609 cp->regs + REG_MAC_RX_CFG);
3611 writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
3614 /* minimum size gigabit frame at half duplex */
3615 cp->min_frame_size = CAS_1000MB_MIN_FRAME;
3618 writel(val, cp->regs + REG_MAC_TX_CFG);
3620 /* checksum bug workaround. don't strip FCS when in
3623 val = readl(cp->regs + REG_MAC_RX_CFG);
3625 val |= MAC_RX_CFG_STRIP_FCS;
3627 cp->min_frame_size = CAS_MIN_MTU;
3629 val &= ~MAC_RX_CFG_STRIP_FCS;
3631 cp->min_frame_size = CAS_MIN_FRAME;
3633 writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
3634 cp->regs + REG_MAC_RX_CFG);
3635 writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
3638 if (netif_msg_link(cp)) {
3640 netdev_info(cp->dev, "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
3644 } else if (pause & 0x10) {
3645 netdev_info(cp->dev, "TX pause enabled\n");
3647 netdev_info(cp->dev, "Pause is disabled\n");
3651 val = readl(cp->regs + REG_MAC_CTRL_CFG);
3652 val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
3653 if (pause) { /* symmetric or asymmetric pause */
3654 val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
3655 if (pause & 0x01) { /* symmetric pause */
3656 val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
3659 writel(val, cp->regs + REG_MAC_CTRL_CFG);
3663 /* Must be invoked under cp->lock. */
3664 static void cas_init_hw(struct cas *cp, int restart_link)
3669 cas_init_pause_thresholds(cp);
3674 /* Default aneg parameters */
3675 cp->timer_ticks = 0;
3676 cas_begin_auto_negotiation(cp, NULL);
3677 } else if (cp->lstate == link_up) {
3678 cas_set_link_modes(cp);
3679 netif_carrier_on(cp->dev);
3683 /* Must be invoked under cp->lock. on earlier cassini boards,
3684 * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
3685 * let it settle out, and then restore pci state.
3687 static void cas_hard_reset(struct cas *cp)
3689 writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
3691 pci_restore_state(cp->pdev);
3695 static void cas_global_reset(struct cas *cp, int blkflag)
3699 /* issue a global reset. don't use RSTOUT. */
3700 if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
3701 /* For PCS, when the blkflag is set, we should set the
3702 * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
3703 * the last autonegotiation from being cleared. We'll
3704 * need some special handling if the chip is set into a
3707 writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
3708 cp->regs + REG_SW_RESET);
3710 writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
3713 /* need to wait at least 3ms before polling register */
3717 while (limit-- > 0) {
3718 u32 val = readl(cp->regs + REG_SW_RESET);
3719 if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
3723 netdev_err(cp->dev, "sw reset failed\n");
3726 /* enable various BIM interrupts */
3727 writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
3728 BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
3730 /* clear out pci error status mask for handled errors.
3731 * we don't deal with DMA counter overflows as they happen
3734 writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
3735 PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
3736 PCI_ERR_BIM_DMA_READ), cp->regs +
3737 REG_PCI_ERR_STATUS_MASK);
3739 /* set up for MII by default to address mac rx reset timeout
3742 writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3745 static void cas_reset(struct cas *cp, int blkflag)
3750 cas_global_reset(cp, blkflag);
3752 cas_entropy_reset(cp);
3754 /* disable dma engines. */
3755 val = readl(cp->regs + REG_TX_CFG);
3756 val &= ~TX_CFG_DMA_EN;
3757 writel(val, cp->regs + REG_TX_CFG);
3759 val = readl(cp->regs + REG_RX_CFG);
3760 val &= ~RX_CFG_DMA_EN;
3761 writel(val, cp->regs + REG_RX_CFG);
3763 /* program header parser */
3764 if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
3765 (&CAS_HP_ALT_FIRMWARE[0] == &cas_prog_null[0])) {
3766 cas_load_firmware(cp, CAS_HP_FIRMWARE);
3768 cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
3771 /* clear out error registers */
3772 spin_lock(&cp->stat_lock[N_TX_RINGS]);
3773 cas_clear_mac_err(cp);
3774 spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3777 /* Shut down the chip, must be called with pm_mutex held. */
3778 static void cas_shutdown(struct cas *cp)
3780 unsigned long flags;
3782 /* Make us not-running to avoid timers respawning */
3785 del_timer_sync(&cp->link_timer);
3787 /* Stop the reset task */
3789 while (atomic_read(&cp->reset_task_pending_mtu) ||
3790 atomic_read(&cp->reset_task_pending_spare) ||
3791 atomic_read(&cp->reset_task_pending_all))
3795 while (atomic_read(&cp->reset_task_pending))
3798 /* Actually stop the chip */
3799 cas_lock_all_save(cp, flags);
3801 if (cp->cas_flags & CAS_FLAG_SATURN)
3802 cas_phy_powerdown(cp);
3803 cas_unlock_all_restore(cp, flags);
3806 static int cas_change_mtu(struct net_device *dev, int new_mtu)
3808 struct cas *cp = netdev_priv(dev);
3811 if (!netif_running(dev) || !netif_device_present(dev))
3814 /* let the reset task handle it */
3816 atomic_inc(&cp->reset_task_pending);
3817 if ((cp->phy_type & CAS_PHY_SERDES)) {
3818 atomic_inc(&cp->reset_task_pending_all);
3820 atomic_inc(&cp->reset_task_pending_mtu);
3822 schedule_work(&cp->reset_task);
3824 atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
3825 CAS_RESET_ALL : CAS_RESET_MTU);
3826 pr_err("reset called in cas_change_mtu\n");
3827 schedule_work(&cp->reset_task);
3830 flush_work(&cp->reset_task);
3834 static void cas_clean_txd(struct cas *cp, int ring)
3836 struct cas_tx_desc *txd = cp->init_txds[ring];
3837 struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
3841 size = TX_DESC_RINGN_SIZE(ring);
3842 for (i = 0; i < size; i++) {
3845 if (skbs[i] == NULL)
3851 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
3852 int ent = i & (size - 1);
3854 /* first buffer is never a tiny buffer and so
3855 * needs to be unmapped.
3857 daddr = le64_to_cpu(txd[ent].buffer);
3858 dlen = CAS_VAL(TX_DESC_BUFLEN,
3859 le64_to_cpu(txd[ent].control));
3860 dma_unmap_page(&cp->pdev->dev, daddr, dlen,
3863 if (frag != skb_shinfo(skb)->nr_frags) {
3866 /* next buffer might by a tiny buffer.
3869 ent = i & (size - 1);
3870 if (cp->tx_tiny_use[ring][ent].used)
3874 dev_kfree_skb_any(skb);
3877 /* zero out tiny buf usage */
3878 memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
3881 /* freed on close */
3882 static inline void cas_free_rx_desc(struct cas *cp, int ring)
3884 cas_page_t **page = cp->rx_pages[ring];
3887 size = RX_DESC_RINGN_SIZE(ring);
3888 for (i = 0; i < size; i++) {
3890 cas_page_free(cp, page[i]);
3896 static void cas_free_rxds(struct cas *cp)
3900 for (i = 0; i < N_RX_DESC_RINGS; i++)
3901 cas_free_rx_desc(cp, i);
3904 /* Must be invoked under cp->lock. */
3905 static void cas_clean_rings(struct cas *cp)
3909 /* need to clean all tx rings */
3910 memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
3911 memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
3912 for (i = 0; i < N_TX_RINGS; i++)
3913 cas_clean_txd(cp, i);
3915 /* zero out init block */
3916 memset(cp->init_block, 0, sizeof(struct cas_init_block));
3921 /* allocated on open */
3922 static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
3924 cas_page_t **page = cp->rx_pages[ring];
3927 size = RX_DESC_RINGN_SIZE(ring);
3928 for (i = 0; i < size; i++) {
3929 if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
3935 static int cas_alloc_rxds(struct cas *cp)
3939 for (i = 0; i < N_RX_DESC_RINGS; i++) {
3940 if (cas_alloc_rx_desc(cp, i) < 0) {
3948 static void cas_reset_task(struct work_struct *work)
3950 struct cas *cp = container_of(work, struct cas, reset_task);
3952 int pending = atomic_read(&cp->reset_task_pending);
3954 int pending_all = atomic_read(&cp->reset_task_pending_all);
3955 int pending_spare = atomic_read(&cp->reset_task_pending_spare);
3956 int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
3958 if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
3959 /* We can have more tasks scheduled than actually
3962 atomic_dec(&cp->reset_task_pending);
3966 /* The link went down, we reset the ring, but keep
3967 * DMA stopped. Use this function for reset
3970 if (cp->hw_running) {
3971 unsigned long flags;
3973 /* Make sure we don't get interrupts or tx packets */
3974 netif_device_detach(cp->dev);
3975 cas_lock_all_save(cp, flags);
3978 /* We call cas_spare_recover when we call cas_open.
3979 * but we do not initialize the lists cas_spare_recover
3980 * uses until cas_open is called.
3982 cas_spare_recover(cp, GFP_ATOMIC);
3985 /* test => only pending_spare set */
3986 if (!pending_all && !pending_mtu)
3989 if (pending == CAS_RESET_SPARE)
3992 /* when pending == CAS_RESET_ALL, the following
3993 * call to cas_init_hw will restart auto negotiation.
3994 * Setting the second argument of cas_reset to
3995 * !(pending == CAS_RESET_ALL) will set this argument
3996 * to 1 (avoiding reinitializing the PHY for the normal
3997 * PCS case) when auto negotiation is not restarted.
4000 cas_reset(cp, !(pending_all > 0));
4002 cas_clean_rings(cp);
4003 cas_init_hw(cp, (pending_all > 0));
4005 cas_reset(cp, !(pending == CAS_RESET_ALL));
4007 cas_clean_rings(cp);
4008 cas_init_hw(cp, pending == CAS_RESET_ALL);
4012 cas_unlock_all_restore(cp, flags);
4013 netif_device_attach(cp->dev);
4016 atomic_sub(pending_all, &cp->reset_task_pending_all);
4017 atomic_sub(pending_spare, &cp->reset_task_pending_spare);
4018 atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
4019 atomic_dec(&cp->reset_task_pending);
4021 atomic_set(&cp->reset_task_pending, 0);
4025 static void cas_link_timer(struct timer_list *t)
4027 struct cas *cp = from_timer(cp, t, link_timer);
4028 int mask, pending = 0, reset = 0;
4029 unsigned long flags;
4031 if (link_transition_timeout != 0 &&
4032 cp->link_transition_jiffies_valid &&
4033 time_is_before_jiffies(cp->link_transition_jiffies +
4034 link_transition_timeout)) {
4035 /* One-second counter so link-down workaround doesn't
4036 * cause resets to occur so fast as to fool the switch
4037 * into thinking the link is down.
4039 cp->link_transition_jiffies_valid = 0;
4042 if (!cp->hw_running)
4045 spin_lock_irqsave(&cp->lock, flags);
4047 cas_entropy_gather(cp);
4049 /* If the link task is still pending, we just
4050 * reschedule the link timer
4053 if (atomic_read(&cp->reset_task_pending_all) ||
4054 atomic_read(&cp->reset_task_pending_spare) ||
4055 atomic_read(&cp->reset_task_pending_mtu))
4058 if (atomic_read(&cp->reset_task_pending))
4062 /* check for rx cleaning */
4063 if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
4066 for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
4067 rmask = CAS_FLAG_RXD_POST(i);
4068 if ((mask & rmask) == 0)
4071 /* post_rxds will do a mod_timer */
4072 if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
4076 cp->cas_flags &= ~rmask;
4080 if (CAS_PHY_MII(cp->phy_type)) {
4082 cas_mif_poll(cp, 0);
4083 bmsr = cas_phy_read(cp, MII_BMSR);
4084 /* WTZ: Solaris driver reads this twice, but that
4085 * may be due to the PCS case and the use of a
4086 * common implementation. Read it twice here to be
4089 bmsr = cas_phy_read(cp, MII_BMSR);
4090 cas_mif_poll(cp, 1);
4091 readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
4092 reset = cas_mii_link_check(cp, bmsr);
4094 reset = cas_pcs_link_check(cp);
4100 /* check for tx state machine confusion */
4101 if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
4102 u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
4104 int tlm = CAS_VAL(MAC_SM_TLM, val);
4106 if (((tlm == 0x5) || (tlm == 0x3)) &&
4107 (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
4108 netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4109 "tx err: MAC_STATE[%08x]\n", val);
4114 val = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
4115 wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
4116 rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
4117 if ((val == 0) && (wptr != rptr)) {
4118 netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4119 "tx err: TX_FIFO[%08x:%08x:%08x]\n",
4131 atomic_inc(&cp->reset_task_pending);
4132 atomic_inc(&cp->reset_task_pending_all);
4133 schedule_work(&cp->reset_task);
4135 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
4136 pr_err("reset called in cas_link_timer\n");
4137 schedule_work(&cp->reset_task);
4142 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
4144 spin_unlock_irqrestore(&cp->lock, flags);
4147 /* tiny buffers are used to avoid target abort issues with
4150 static void cas_tx_tiny_free(struct cas *cp)
4152 struct pci_dev *pdev = cp->pdev;
4155 for (i = 0; i < N_TX_RINGS; i++) {
4156 if (!cp->tx_tiny_bufs[i])
4159 dma_free_coherent(&pdev->dev, TX_TINY_BUF_BLOCK,
4160 cp->tx_tiny_bufs[i], cp->tx_tiny_dvma[i]);
4161 cp->tx_tiny_bufs[i] = NULL;
4165 static int cas_tx_tiny_alloc(struct cas *cp)
4167 struct pci_dev *pdev = cp->pdev;
4170 for (i = 0; i < N_TX_RINGS; i++) {
4171 cp->tx_tiny_bufs[i] =
4172 dma_alloc_coherent(&pdev->dev, TX_TINY_BUF_BLOCK,
4173 &cp->tx_tiny_dvma[i], GFP_KERNEL);
4174 if (!cp->tx_tiny_bufs[i]) {
4175 cas_tx_tiny_free(cp);
4183 static int cas_open(struct net_device *dev)
4185 struct cas *cp = netdev_priv(dev);
4187 unsigned long flags;
4189 mutex_lock(&cp->pm_mutex);
4191 hw_was_up = cp->hw_running;
4193 /* The power-management mutex protects the hw_running
4194 * etc. state so it is safe to do this bit without cp->lock
4196 if (!cp->hw_running) {
4197 /* Reset the chip */
4198 cas_lock_all_save(cp, flags);
4199 /* We set the second arg to cas_reset to zero
4200 * because cas_init_hw below will have its second
4201 * argument set to non-zero, which will force
4202 * autonegotiation to start.
4206 cas_unlock_all_restore(cp, flags);
4210 if (cas_tx_tiny_alloc(cp) < 0)
4213 /* alloc rx descriptors */
4214 if (cas_alloc_rxds(cp) < 0)
4217 /* allocate spares */
4219 cas_spare_recover(cp, GFP_KERNEL);
4221 /* We can now request the interrupt as we know it's masked
4222 * on the controller. cassini+ has up to 4 interrupts
4223 * that can be used, but you need to do explicit pci interrupt
4224 * mapping to expose them
4226 if (request_irq(cp->pdev->irq, cas_interrupt,
4227 IRQF_SHARED, dev->name, (void *) dev)) {
4228 netdev_err(cp->dev, "failed to request irq !\n");
4234 napi_enable(&cp->napi);
4237 cas_lock_all_save(cp, flags);
4238 cas_clean_rings(cp);
4239 cas_init_hw(cp, !hw_was_up);
4241 cas_unlock_all_restore(cp, flags);
4243 netif_start_queue(dev);
4244 mutex_unlock(&cp->pm_mutex);
4251 cas_tx_tiny_free(cp);
4253 mutex_unlock(&cp->pm_mutex);
4257 static int cas_close(struct net_device *dev)
4259 unsigned long flags;
4260 struct cas *cp = netdev_priv(dev);
4263 napi_disable(&cp->napi);
4265 /* Make sure we don't get distracted by suspend/resume */
4266 mutex_lock(&cp->pm_mutex);
4268 netif_stop_queue(dev);
4270 /* Stop traffic, mark us closed */
4271 cas_lock_all_save(cp, flags);
4275 cas_begin_auto_negotiation(cp, NULL);
4276 cas_clean_rings(cp);
4277 cas_unlock_all_restore(cp, flags);
4279 free_irq(cp->pdev->irq, (void *) dev);
4282 cas_tx_tiny_free(cp);
4283 mutex_unlock(&cp->pm_mutex);
4288 const char name[ETH_GSTRING_LEN];
4289 } ethtool_cassini_statnames[] = {
4296 {"rx_frame_errors"},
4297 {"rx_length_errors"},
4300 {"tx_aborted_errors"},
4307 #define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
4310 const int offsets; /* neg. values for 2nd arg to cas_read_phy */
4311 } ethtool_register_table[] = {
4326 {REG_PCS_MII_STATUS},
4327 {REG_PCS_STATE_MACHINE},
4328 {REG_MAC_COLL_EXCESS},
4331 #define CAS_REG_LEN ARRAY_SIZE(ethtool_register_table)
4332 #define CAS_MAX_REGS (sizeof (u32)*CAS_REG_LEN)
4334 static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
4338 unsigned long flags;
4340 spin_lock_irqsave(&cp->lock, flags);
4341 for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
4344 if (ethtool_register_table[i].offsets < 0) {
4345 hval = cas_phy_read(cp,
4346 -ethtool_register_table[i].offsets);
4349 val= readl(cp->regs+ethtool_register_table[i].offsets);
4351 memcpy(p, (u8 *)&val, sizeof(u32));
4353 spin_unlock_irqrestore(&cp->lock, flags);
4356 static struct net_device_stats *cas_get_stats(struct net_device *dev)
4358 struct cas *cp = netdev_priv(dev);
4359 struct net_device_stats *stats = cp->net_stats;
4360 unsigned long flags;
4364 /* we collate all of the stats into net_stats[N_TX_RING] */
4365 if (!cp->hw_running)
4366 return stats + N_TX_RINGS;
4368 /* collect outstanding stats */
4369 /* WTZ: the Cassini spec gives these as 16 bit counters but
4370 * stored in 32-bit words. Added a mask of 0xffff to be safe,
4371 * in case the chip somehow puts any garbage in the other bits.
4372 * Also, counter usage didn't seem to mach what Adrian did
4373 * in the parts of the code that set these quantities. Made
4376 spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
4377 stats[N_TX_RINGS].rx_crc_errors +=
4378 readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
4379 stats[N_TX_RINGS].rx_frame_errors +=
4380 readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
4381 stats[N_TX_RINGS].rx_length_errors +=
4382 readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
4384 tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
4385 (readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
4386 stats[N_TX_RINGS].tx_aborted_errors += tmp;
4387 stats[N_TX_RINGS].collisions +=
4388 tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
4390 stats[N_TX_RINGS].tx_aborted_errors +=
4391 readl(cp->regs + REG_MAC_COLL_EXCESS);
4392 stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
4393 readl(cp->regs + REG_MAC_COLL_LATE);
4395 cas_clear_mac_err(cp);
4397 /* saved bits that are unique to ring 0 */
4398 spin_lock(&cp->stat_lock[0]);
4399 stats[N_TX_RINGS].collisions += stats[0].collisions;
4400 stats[N_TX_RINGS].rx_over_errors += stats[0].rx_over_errors;
4401 stats[N_TX_RINGS].rx_frame_errors += stats[0].rx_frame_errors;
4402 stats[N_TX_RINGS].rx_fifo_errors += stats[0].rx_fifo_errors;
4403 stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
4404 stats[N_TX_RINGS].tx_fifo_errors += stats[0].tx_fifo_errors;
4405 spin_unlock(&cp->stat_lock[0]);
4407 for (i = 0; i < N_TX_RINGS; i++) {
4408 spin_lock(&cp->stat_lock[i]);
4409 stats[N_TX_RINGS].rx_length_errors +=
4410 stats[i].rx_length_errors;
4411 stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
4412 stats[N_TX_RINGS].rx_packets += stats[i].rx_packets;
4413 stats[N_TX_RINGS].tx_packets += stats[i].tx_packets;
4414 stats[N_TX_RINGS].rx_bytes += stats[i].rx_bytes;
4415 stats[N_TX_RINGS].tx_bytes += stats[i].tx_bytes;
4416 stats[N_TX_RINGS].rx_errors += stats[i].rx_errors;
4417 stats[N_TX_RINGS].tx_errors += stats[i].tx_errors;
4418 stats[N_TX_RINGS].rx_dropped += stats[i].rx_dropped;
4419 stats[N_TX_RINGS].tx_dropped += stats[i].tx_dropped;
4420 memset(stats + i, 0, sizeof(struct net_device_stats));
4421 spin_unlock(&cp->stat_lock[i]);
4423 spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
4424 return stats + N_TX_RINGS;
4428 static void cas_set_multicast(struct net_device *dev)
4430 struct cas *cp = netdev_priv(dev);
4431 u32 rxcfg, rxcfg_new;
4432 unsigned long flags;
4433 int limit = STOP_TRIES;
4435 if (!cp->hw_running)
4438 spin_lock_irqsave(&cp->lock, flags);
4439 rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
4441 /* disable RX MAC and wait for completion */
4442 writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4443 while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
4449 /* disable hash filter and wait for completion */
4451 rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
4452 writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4453 while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
4459 /* program hash filters */
4460 cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
4462 writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
4463 spin_unlock_irqrestore(&cp->lock, flags);
4466 static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
4468 struct cas *cp = netdev_priv(dev);
4469 strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
4470 strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
4471 strscpy(info->bus_info, pci_name(cp->pdev), sizeof(info->bus_info));
4474 static int cas_get_link_ksettings(struct net_device *dev,
4475 struct ethtool_link_ksettings *cmd)
4477 struct cas *cp = netdev_priv(dev);
4479 int full_duplex, speed, pause;
4480 unsigned long flags;
4481 enum link_state linkstate = link_up;
4482 u32 supported, advertising;
4485 supported = SUPPORTED_Autoneg;
4486 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
4487 supported |= SUPPORTED_1000baseT_Full;
4488 advertising |= ADVERTISED_1000baseT_Full;
4491 /* Record PHY settings if HW is on. */
4492 spin_lock_irqsave(&cp->lock, flags);
4494 linkstate = cp->lstate;
4495 if (CAS_PHY_MII(cp->phy_type)) {
4496 cmd->base.port = PORT_MII;
4497 cmd->base.phy_address = cp->phy_addr;
4498 advertising |= ADVERTISED_TP | ADVERTISED_MII |
4499 ADVERTISED_10baseT_Half |
4500 ADVERTISED_10baseT_Full |
4501 ADVERTISED_100baseT_Half |
4502 ADVERTISED_100baseT_Full;
4505 (SUPPORTED_10baseT_Half |
4506 SUPPORTED_10baseT_Full |
4507 SUPPORTED_100baseT_Half |
4508 SUPPORTED_100baseT_Full |
4509 SUPPORTED_TP | SUPPORTED_MII);
4511 if (cp->hw_running) {
4512 cas_mif_poll(cp, 0);
4513 bmcr = cas_phy_read(cp, MII_BMCR);
4514 cas_read_mii_link_mode(cp, &full_duplex,
4516 cas_mif_poll(cp, 1);
4520 cmd->base.port = PORT_FIBRE;
4521 cmd->base.phy_address = 0;
4522 supported |= SUPPORTED_FIBRE;
4523 advertising |= ADVERTISED_FIBRE;
4525 if (cp->hw_running) {
4526 /* pcs uses the same bits as mii */
4527 bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
4528 cas_read_pcs_link_mode(cp, &full_duplex,
4532 spin_unlock_irqrestore(&cp->lock, flags);
4534 if (bmcr & BMCR_ANENABLE) {
4535 advertising |= ADVERTISED_Autoneg;
4536 cmd->base.autoneg = AUTONEG_ENABLE;
4537 cmd->base.speed = ((speed == 10) ?
4540 SPEED_1000 : SPEED_100));
4541 cmd->base.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
4543 cmd->base.autoneg = AUTONEG_DISABLE;
4544 cmd->base.speed = ((bmcr & CAS_BMCR_SPEED1000) ?
4546 ((bmcr & BMCR_SPEED100) ?
4547 SPEED_100 : SPEED_10));
4548 cmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
4549 DUPLEX_FULL : DUPLEX_HALF;
4551 if (linkstate != link_up) {
4552 /* Force these to "unknown" if the link is not up and
4553 * autonogotiation in enabled. We can set the link
4554 * speed to 0, but not cmd->duplex,
4555 * because its legal values are 0 and 1. Ethtool will
4556 * print the value reported in parentheses after the
4557 * word "Unknown" for unrecognized values.
4559 * If in forced mode, we report the speed and duplex
4560 * settings that we configured.
4562 if (cp->link_cntl & BMCR_ANENABLE) {
4563 cmd->base.speed = 0;
4564 cmd->base.duplex = 0xff;
4566 cmd->base.speed = SPEED_10;
4567 if (cp->link_cntl & BMCR_SPEED100) {
4568 cmd->base.speed = SPEED_100;
4569 } else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
4570 cmd->base.speed = SPEED_1000;
4572 cmd->base.duplex = (cp->link_cntl & BMCR_FULLDPLX) ?
4573 DUPLEX_FULL : DUPLEX_HALF;
4577 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
4579 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
4585 static int cas_set_link_ksettings(struct net_device *dev,
4586 const struct ethtool_link_ksettings *cmd)
4588 struct cas *cp = netdev_priv(dev);
4589 unsigned long flags;
4590 u32 speed = cmd->base.speed;
4592 /* Verify the settings we care about. */
4593 if (cmd->base.autoneg != AUTONEG_ENABLE &&
4594 cmd->base.autoneg != AUTONEG_DISABLE)
4597 if (cmd->base.autoneg == AUTONEG_DISABLE &&
4598 ((speed != SPEED_1000 &&
4599 speed != SPEED_100 &&
4600 speed != SPEED_10) ||
4601 (cmd->base.duplex != DUPLEX_HALF &&
4602 cmd->base.duplex != DUPLEX_FULL)))
4605 /* Apply settings and restart link process. */
4606 spin_lock_irqsave(&cp->lock, flags);
4607 cas_begin_auto_negotiation(cp, cmd);
4608 spin_unlock_irqrestore(&cp->lock, flags);
4612 static int cas_nway_reset(struct net_device *dev)
4614 struct cas *cp = netdev_priv(dev);
4615 unsigned long flags;
4617 if ((cp->link_cntl & BMCR_ANENABLE) == 0)
4620 /* Restart link process. */
4621 spin_lock_irqsave(&cp->lock, flags);
4622 cas_begin_auto_negotiation(cp, NULL);
4623 spin_unlock_irqrestore(&cp->lock, flags);
4628 static u32 cas_get_link(struct net_device *dev)
4630 struct cas *cp = netdev_priv(dev);
4631 return cp->lstate == link_up;
4634 static u32 cas_get_msglevel(struct net_device *dev)
4636 struct cas *cp = netdev_priv(dev);
4637 return cp->msg_enable;
4640 static void cas_set_msglevel(struct net_device *dev, u32 value)
4642 struct cas *cp = netdev_priv(dev);
4643 cp->msg_enable = value;
4646 static int cas_get_regs_len(struct net_device *dev)
4648 struct cas *cp = netdev_priv(dev);
4649 return min_t(int, cp->casreg_len, CAS_MAX_REGS);
4652 static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
4655 struct cas *cp = netdev_priv(dev);
4657 /* cas_read_regs handles locks (cp->lock). */
4658 cas_read_regs(cp, p, regs->len / sizeof(u32));
4661 static int cas_get_sset_count(struct net_device *dev, int sset)
4665 return CAS_NUM_STAT_KEYS;
4671 static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4673 memcpy(data, ðtool_cassini_statnames,
4674 CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
4677 static void cas_get_ethtool_stats(struct net_device *dev,
4678 struct ethtool_stats *estats, u64 *data)
4680 struct cas *cp = netdev_priv(dev);
4681 struct net_device_stats *stats = cas_get_stats(cp->dev);
4683 data[i++] = stats->collisions;
4684 data[i++] = stats->rx_bytes;
4685 data[i++] = stats->rx_crc_errors;
4686 data[i++] = stats->rx_dropped;
4687 data[i++] = stats->rx_errors;
4688 data[i++] = stats->rx_fifo_errors;
4689 data[i++] = stats->rx_frame_errors;
4690 data[i++] = stats->rx_length_errors;
4691 data[i++] = stats->rx_over_errors;
4692 data[i++] = stats->rx_packets;
4693 data[i++] = stats->tx_aborted_errors;
4694 data[i++] = stats->tx_bytes;
4695 data[i++] = stats->tx_dropped;
4696 data[i++] = stats->tx_errors;
4697 data[i++] = stats->tx_fifo_errors;
4698 data[i++] = stats->tx_packets;
4699 BUG_ON(i != CAS_NUM_STAT_KEYS);
4702 static const struct ethtool_ops cas_ethtool_ops = {
4703 .get_drvinfo = cas_get_drvinfo,
4704 .nway_reset = cas_nway_reset,
4705 .get_link = cas_get_link,
4706 .get_msglevel = cas_get_msglevel,
4707 .set_msglevel = cas_set_msglevel,
4708 .get_regs_len = cas_get_regs_len,
4709 .get_regs = cas_get_regs,
4710 .get_sset_count = cas_get_sset_count,
4711 .get_strings = cas_get_strings,
4712 .get_ethtool_stats = cas_get_ethtool_stats,
4713 .get_link_ksettings = cas_get_link_ksettings,
4714 .set_link_ksettings = cas_set_link_ksettings,
4717 static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4719 struct cas *cp = netdev_priv(dev);
4720 struct mii_ioctl_data *data = if_mii(ifr);
4721 unsigned long flags;
4722 int rc = -EOPNOTSUPP;
4724 /* Hold the PM mutex while doing ioctl's or we may collide
4725 * with open/close and power management and oops.
4727 mutex_lock(&cp->pm_mutex);
4729 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
4730 data->phy_id = cp->phy_addr;
4733 case SIOCGMIIREG: /* Read MII PHY register. */
4734 spin_lock_irqsave(&cp->lock, flags);
4735 cas_mif_poll(cp, 0);
4736 data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
4737 cas_mif_poll(cp, 1);
4738 spin_unlock_irqrestore(&cp->lock, flags);
4742 case SIOCSMIIREG: /* Write MII PHY register. */
4743 spin_lock_irqsave(&cp->lock, flags);
4744 cas_mif_poll(cp, 0);
4745 rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
4746 cas_mif_poll(cp, 1);
4747 spin_unlock_irqrestore(&cp->lock, flags);
4753 mutex_unlock(&cp->pm_mutex);
4757 /* When this chip sits underneath an Intel 31154 bridge, it is the
4758 * only subordinate device and we can tweak the bridge settings to
4759 * reflect that fact.
4761 static void cas_program_bridge(struct pci_dev *cas_pdev)
4763 struct pci_dev *pdev = cas_pdev->bus->self;
4769 if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
4772 /* Clear bit 10 (Bus Parking Control) in the Secondary
4773 * Arbiter Control/Status Register which lives at offset
4774 * 0x41. Using a 32-bit word read/modify/write at 0x40
4775 * is much simpler so that's how we do this.
4777 pci_read_config_dword(pdev, 0x40, &val);
4779 pci_write_config_dword(pdev, 0x40, val);
4781 /* Max out the Multi-Transaction Timer settings since
4782 * Cassini is the only device present.
4784 * The register is 16-bit and lives at 0x50. When the
4785 * settings are enabled, it extends the GRANT# signal
4786 * for a requestor after a transaction is complete. This
4787 * allows the next request to run without first needing
4788 * to negotiate the GRANT# signal back.
4790 * Bits 12:10 define the grant duration:
4798 * All other values are illegal.
4800 * Bits 09:00 define which REQ/GNT signal pairs get the
4801 * GRANT# signal treatment. We set them all.
4803 pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
4805 /* The Read Prefecth Policy register is 16-bit and sits at
4806 * offset 0x52. It enables a "smart" pre-fetch policy. We
4807 * enable it and max out all of the settings since only one
4808 * device is sitting underneath and thus bandwidth sharing is
4811 * The register has several 3 bit fields, which indicates a
4812 * multiplier applied to the base amount of prefetching the
4813 * chip would do. These fields are at:
4815 * 15:13 --- ReRead Primary Bus
4816 * 12:10 --- FirstRead Primary Bus
4817 * 09:07 --- ReRead Secondary Bus
4818 * 06:04 --- FirstRead Secondary Bus
4820 * Bits 03:00 control which REQ/GNT pairs the prefetch settings
4821 * get enabled on. Bit 3 is a grouped enabler which controls
4822 * all of the REQ/GNT pairs from [8:3]. Bits 2 to 0 control
4823 * the individual REQ/GNT pairs [2:0].
4825 pci_write_config_word(pdev, 0x52,
4832 /* Force cacheline size to 0x8 */
4833 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4835 /* Force latency timer to maximum setting so Cassini can
4836 * sit on the bus as long as it likes.
4838 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
4841 static const struct net_device_ops cas_netdev_ops = {
4842 .ndo_open = cas_open,
4843 .ndo_stop = cas_close,
4844 .ndo_start_xmit = cas_start_xmit,
4845 .ndo_get_stats = cas_get_stats,
4846 .ndo_set_rx_mode = cas_set_multicast,
4847 .ndo_eth_ioctl = cas_ioctl,
4848 .ndo_tx_timeout = cas_tx_timeout,
4849 .ndo_change_mtu = cas_change_mtu,
4850 .ndo_set_mac_address = eth_mac_addr,
4851 .ndo_validate_addr = eth_validate_addr,
4852 #ifdef CONFIG_NET_POLL_CONTROLLER
4853 .ndo_poll_controller = cas_netpoll,
4857 static int cas_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4859 static int cas_version_printed = 0;
4860 unsigned long casreg_len;
4861 struct net_device *dev;
4865 u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
4867 if (cas_version_printed++ == 0)
4868 pr_info("%s", version);
4870 err = pci_enable_device(pdev);
4872 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
4876 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
4877 dev_err(&pdev->dev, "Cannot find proper PCI device "
4878 "base address, aborting\n");
4880 goto err_out_disable_pdev;
4883 dev = alloc_etherdev(sizeof(*cp));
4886 goto err_out_disable_pdev;
4888 SET_NETDEV_DEV(dev, &pdev->dev);
4890 err = pci_request_regions(pdev, dev->name);
4892 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
4893 goto err_out_free_netdev;
4895 pci_set_master(pdev);
4897 /* we must always turn on parity response or else parity
4898 * doesn't get generated properly. disable SERR/PERR as well.
4899 * in addition, we want to turn MWI on.
4901 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4902 pci_cmd &= ~PCI_COMMAND_SERR;
4903 pci_cmd |= PCI_COMMAND_PARITY;
4904 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4905 if (pci_try_set_mwi(pdev))
4906 pr_warn("Could not enable MWI for %s\n", pci_name(pdev));
4908 cas_program_bridge(pdev);
4911 * On some architectures, the default cache line size set
4912 * by pci_try_set_mwi reduces perforamnce. We have to increase
4913 * it for this case. To start, we'll print some configuration
4917 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
4918 &orig_cacheline_size);
4919 if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
4920 cas_cacheline_size =
4921 (CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
4922 CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
4923 if (pci_write_config_byte(pdev,
4924 PCI_CACHE_LINE_SIZE,
4925 cas_cacheline_size)) {
4926 dev_err(&pdev->dev, "Could not set PCI cache "
4928 goto err_out_free_res;
4934 /* Configure DMA attributes. */
4935 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4937 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4938 goto err_out_free_res;
4941 casreg_len = pci_resource_len(pdev, 0);
4943 cp = netdev_priv(dev);
4946 /* A value of 0 indicates we never explicitly set it */
4947 cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
4950 cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
4953 #if defined(CONFIG_SPARC)
4954 cp->of_node = pci_device_to_OF_node(pdev);
4957 cp->link_transition = LINK_TRANSITION_UNKNOWN;
4958 cp->link_transition_jiffies_valid = 0;
4960 spin_lock_init(&cp->lock);
4961 spin_lock_init(&cp->rx_inuse_lock);
4962 spin_lock_init(&cp->rx_spare_lock);
4963 for (i = 0; i < N_TX_RINGS; i++) {
4964 spin_lock_init(&cp->stat_lock[i]);
4965 spin_lock_init(&cp->tx_lock[i]);
4967 spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
4968 mutex_init(&cp->pm_mutex);
4970 timer_setup(&cp->link_timer, cas_link_timer, 0);
4973 /* Just in case the implementation of atomic operations
4974 * change so that an explicit initialization is necessary.
4976 atomic_set(&cp->reset_task_pending, 0);
4977 atomic_set(&cp->reset_task_pending_all, 0);
4978 atomic_set(&cp->reset_task_pending_spare, 0);
4979 atomic_set(&cp->reset_task_pending_mtu, 0);
4981 INIT_WORK(&cp->reset_task, cas_reset_task);
4983 /* Default link parameters */
4984 if (link_mode >= 0 && link_mode < 6)
4985 cp->link_cntl = link_modes[link_mode];
4987 cp->link_cntl = BMCR_ANENABLE;
4988 cp->lstate = link_down;
4989 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
4990 netif_carrier_off(cp->dev);
4991 cp->timer_ticks = 0;
4993 /* give us access to cassini registers */
4994 cp->regs = pci_iomap(pdev, 0, casreg_len);
4996 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
4997 goto err_out_free_res;
4999 cp->casreg_len = casreg_len;
5001 pci_save_state(pdev);
5002 cas_check_pci_invariants(cp);
5005 if (cas_check_invariants(cp))
5006 goto err_out_iounmap;
5007 if (cp->cas_flags & CAS_FLAG_SATURN)
5008 cas_saturn_firmware_init(cp);
5011 dma_alloc_coherent(&pdev->dev, sizeof(struct cas_init_block),
5012 &cp->block_dvma, GFP_KERNEL);
5013 if (!cp->init_block) {
5014 dev_err(&pdev->dev, "Cannot allocate init block, aborting\n");
5015 goto err_out_iounmap;
5018 for (i = 0; i < N_TX_RINGS; i++)
5019 cp->init_txds[i] = cp->init_block->txds[i];
5021 for (i = 0; i < N_RX_DESC_RINGS; i++)
5022 cp->init_rxds[i] = cp->init_block->rxds[i];
5024 for (i = 0; i < N_RX_COMP_RINGS; i++)
5025 cp->init_rxcs[i] = cp->init_block->rxcs[i];
5027 for (i = 0; i < N_RX_FLOWS; i++)
5028 skb_queue_head_init(&cp->rx_flows[i]);
5030 dev->netdev_ops = &cas_netdev_ops;
5031 dev->ethtool_ops = &cas_ethtool_ops;
5032 dev->watchdog_timeo = CAS_TX_TIMEOUT;
5035 netif_napi_add(dev, &cp->napi, cas_poll);
5037 dev->irq = pdev->irq;
5040 /* Cassini features. */
5041 if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
5042 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5044 dev->features |= NETIF_F_HIGHDMA;
5046 /* MTU range: 60 - varies or 9000 */
5047 dev->min_mtu = CAS_MIN_MTU;
5048 dev->max_mtu = CAS_MAX_MTU;
5050 if (register_netdev(dev)) {
5051 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
5052 goto err_out_free_consistent;
5055 i = readl(cp->regs + REG_BIM_CFG);
5056 netdev_info(dev, "Sun Cassini%s (%sbit/%sMHz PCI/%s) Ethernet[%d] %pM\n",
5057 (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
5058 (i & BIM_CFG_32BIT) ? "32" : "64",
5059 (i & BIM_CFG_66MHZ) ? "66" : "33",
5060 (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
5063 pci_set_drvdata(pdev, dev);
5065 cas_entropy_reset(cp);
5067 cas_begin_auto_negotiation(cp, NULL);
5070 err_out_free_consistent:
5071 dma_free_coherent(&pdev->dev, sizeof(struct cas_init_block),
5072 cp->init_block, cp->block_dvma);
5075 mutex_lock(&cp->pm_mutex);
5078 mutex_unlock(&cp->pm_mutex);
5080 pci_iounmap(pdev, cp->regs);
5084 pci_release_regions(pdev);
5086 /* Try to restore it in case the error occurred after we
5089 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
5091 err_out_free_netdev:
5094 err_out_disable_pdev:
5095 pci_disable_device(pdev);
5099 static void cas_remove_one(struct pci_dev *pdev)
5101 struct net_device *dev = pci_get_drvdata(pdev);
5106 cp = netdev_priv(dev);
5107 unregister_netdev(dev);
5111 mutex_lock(&cp->pm_mutex);
5112 cancel_work_sync(&cp->reset_task);
5115 mutex_unlock(&cp->pm_mutex);
5118 if (cp->orig_cacheline_size) {
5119 /* Restore the cache line size if we had modified
5122 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5123 cp->orig_cacheline_size);
5126 dma_free_coherent(&pdev->dev, sizeof(struct cas_init_block),
5127 cp->init_block, cp->block_dvma);
5128 pci_iounmap(pdev, cp->regs);
5130 pci_release_regions(pdev);
5131 pci_disable_device(pdev);
5134 static int __maybe_unused cas_suspend(struct device *dev_d)
5136 struct net_device *dev = dev_get_drvdata(dev_d);
5137 struct cas *cp = netdev_priv(dev);
5138 unsigned long flags;
5140 mutex_lock(&cp->pm_mutex);
5142 /* If the driver is opened, we stop the DMA */
5144 netif_device_detach(dev);
5146 cas_lock_all_save(cp, flags);
5148 /* We can set the second arg of cas_reset to 0
5149 * because on resume, we'll call cas_init_hw with
5150 * its second arg set so that autonegotiation is
5154 cas_clean_rings(cp);
5155 cas_unlock_all_restore(cp, flags);
5160 mutex_unlock(&cp->pm_mutex);
5165 static int __maybe_unused cas_resume(struct device *dev_d)
5167 struct net_device *dev = dev_get_drvdata(dev_d);
5168 struct cas *cp = netdev_priv(dev);
5170 netdev_info(dev, "resuming\n");
5172 mutex_lock(&cp->pm_mutex);
5175 unsigned long flags;
5176 cas_lock_all_save(cp, flags);
5179 cas_clean_rings(cp);
5181 cas_unlock_all_restore(cp, flags);
5183 netif_device_attach(dev);
5185 mutex_unlock(&cp->pm_mutex);
5189 static SIMPLE_DEV_PM_OPS(cas_pm_ops, cas_suspend, cas_resume);
5191 static struct pci_driver cas_driver = {
5192 .name = DRV_MODULE_NAME,
5193 .id_table = cas_pci_tbl,
5194 .probe = cas_init_one,
5195 .remove = cas_remove_one,
5196 .driver.pm = &cas_pm_ops,
5199 static int __init cas_init(void)
5201 if (linkdown_timeout > 0)
5202 link_transition_timeout = linkdown_timeout * HZ;
5204 link_transition_timeout = 0;
5206 return pci_register_driver(&cas_driver);
5209 static void __exit cas_cleanup(void)
5211 pci_unregister_driver(&cas_driver);
5214 module_init(cas_init);
5215 module_exit(cas_cleanup);