blob: c91876f8c536abc88e47e99c47100938e99e396f [file] [log] [blame]
Thomas Gleixner56cb4e52019-01-18 11:49:58 +01001// SPDX-License-Identifier: GPL-2.0+
David S. Miller1f26dac2005-09-27 15:24:13 -07002/* cassini.c: Sun Microsystems Cassini(+) ethernet driver.
3 *
4 * Copyright (C) 2004 Sun Microsystems Inc.
5 * Copyright (C) 2003 Adrian Sun (asun@darksunrising.com)
6 *
David S. Miller1f26dac2005-09-27 15:24:13 -07007 * This driver uses the sungem driver (c) David Miller
8 * (davem@redhat.com) as its basis.
9 *
10 * The cassini chip has a number of features that distinguish it from
11 * the gem chip:
12 * 4 transmit descriptor rings that are used for either QoS (VLAN) or
13 * load balancing (non-VLAN mode)
14 * batching of multiple packets
15 * multiple CPU dispatching
16 * page-based RX descriptor engine with separate completion rings
17 * Gigabit support (GMII and PCS interface)
18 * MIF link up/down detection works
19 *
20 * RX is handled by page sized buffers that are attached as fragments to
21 * the skb. here's what's done:
22 * -- driver allocates pages at a time and keeps reference counts
23 * on them.
24 * -- the upper protocol layers assume that the header is in the skb
25 * itself. as a result, cassini will copy a small amount (64 bytes)
26 * to make them happy.
27 * -- driver appends the rest of the data pages as frags to skbuffs
28 * and increments the reference count
29 * -- on page reclamation, the driver swaps the page with a spare page.
30 * if that page is still in use, it frees its reference to that page,
31 * and allocates a new page for use. otherwise, it just recycles the
Jeff Garzik6aa20a22006-09-13 13:24:59 -040032 * the page.
David S. Miller1f26dac2005-09-27 15:24:13 -070033 *
34 * NOTE: cassini can parse the header. however, it's not worth it
35 * as long as the network stack requires a header copy.
36 *
37 * TX has 4 queues. currently these queues are used in a round-robin
38 * fashion for load balancing. They can also be used for QoS. for that
39 * to work, however, QoS information needs to be exposed down to the driver
Lucas De Marchi25985ed2011-03-30 22:57:33 -030040 * level so that subqueues get targeted to particular transmit rings.
David S. Miller1f26dac2005-09-27 15:24:13 -070041 * alternatively, the queues can be configured via use of the all-purpose
42 * ioctl.
43 *
44 * RX DATA: the rx completion ring has all the info, but the rx desc
45 * ring has all of the data. RX can conceivably come in under multiple
46 * interrupts, but the INT# assignment needs to be set up properly by
47 * the BIOS and conveyed to the driver. PCI BIOSes don't know how to do
48 * that. also, the two descriptor rings are designed to distinguish between
Jeff Garzik6aa20a22006-09-13 13:24:59 -040049 * encrypted and non-encrypted packets, but we use them for buffering
David S. Miller1f26dac2005-09-27 15:24:13 -070050 * instead.
51 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040052 * by default, the selective clear mask is set up to process rx packets.
David S. Miller1f26dac2005-09-27 15:24:13 -070053 */
54
Joe Perches436d27d2010-02-17 15:01:53 +000055#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
David S. Miller1f26dac2005-09-27 15:24:13 -070056
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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000064#include <linux/interrupt.h>
Jaswinder Singhfcaa4062008-09-22 19:27:10 -070065#include <linux/vmalloc.h>
David S. Miller1f26dac2005-09-27 15:24:13 -070066#include <linux/ioport.h>
67#include <linux/pci.h>
68#include <linux/mm.h>
69#include <linux/highmem.h>
70#include <linux/list.h>
71#include <linux/dma-mapping.h>
72
73#include <linux/netdevice.h>
74#include <linux/etherdevice.h>
75#include <linux/skbuff.h>
76#include <linux/ethtool.h>
77#include <linux/crc32.h>
78#include <linux/random.h>
79#include <linux/mii.h>
80#include <linux/ip.h>
81#include <linux/tcp.h>
Ingo Molnar758df692006-03-20 22:34:09 -080082#include <linux/mutex.h>
Jaswinder Singhfcaa4062008-09-22 19:27:10 -070083#include <linux/firmware.h>
David S. Miller1f26dac2005-09-27 15:24:13 -070084
85#include <net/checksum.h>
86
Arun Sharma600634972011-07-26 16:09:06 -070087#include <linux/atomic.h>
David S. Miller1f26dac2005-09-27 15:24:13 -070088#include <asm/io.h>
89#include <asm/byteorder.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080090#include <linux/uaccess.h>
David S. Miller1f26dac2005-09-27 15:24:13 -070091
Cong Wang46790262011-11-25 23:14:23 +080092#define cas_page_map(x) kmap_atomic((x))
93#define cas_page_unmap(x) kunmap_atomic((x))
David S. Miller1f26dac2005-09-27 15:24:13 -070094#define CAS_NCPUS num_online_cpus()
95
David S. Miller1f26dac2005-09-27 15:24:13 -070096#define cas_skb_release(x) netif_rx(x)
David S. Miller1f26dac2005-09-27 15:24:13 -070097
98/* select which firmware to use */
Jeff Garzik6aa20a22006-09-13 13:24:59 -040099#define USE_HP_WORKAROUND
David S. Miller1f26dac2005-09-27 15:24:13 -0700100#define HP_WORKAROUND_DEFAULT /* select which firmware to use as default */
101#define CAS_HP_ALT_FIRMWARE cas_prog_null /* alternate firmware */
102
103#include "cassini.h"
104
105#define USE_TX_COMPWB /* use completion writeback registers */
106#define USE_CSMA_CD_PROTO /* standard CSMA/CD */
107#define USE_RX_BLANK /* hw interrupt mitigation */
108#undef USE_ENTROPY_DEV /* don't test for entropy device */
109
110/* NOTE: these aren't useable unless PCI interrupts can be assigned.
111 * also, we need to make cp->lock finer-grained.
112 */
113#undef USE_PCI_INTB
114#undef USE_PCI_INTC
115#undef USE_PCI_INTD
116#undef USE_QOS
117
118#undef USE_VPD_DEBUG /* debug vpd information if defined */
119
120/* rx processing options */
121#define USE_PAGE_ORDER /* specify to allocate large rx pages */
122#define RX_DONT_BATCH 0 /* if 1, don't batch flows */
123#define RX_COPY_ALWAYS 0 /* if 0, use frags */
124#define RX_COPY_MIN 64 /* copy a little to make upper layers happy */
125#undef RX_COUNT_BUFFERS /* define to calculate RX buffer stats */
126
127#define DRV_MODULE_NAME "cassini"
David S. Millerb1443e22008-05-21 17:05:34 -0700128#define DRV_MODULE_VERSION "1.6"
129#define DRV_MODULE_RELDATE "21 May 2008"
David S. Miller1f26dac2005-09-27 15:24:13 -0700130
131#define CAS_DEF_MSG_ENABLE \
132 (NETIF_MSG_DRV | \
133 NETIF_MSG_PROBE | \
134 NETIF_MSG_LINK | \
135 NETIF_MSG_TIMER | \
136 NETIF_MSG_IFDOWN | \
137 NETIF_MSG_IFUP | \
138 NETIF_MSG_RX_ERR | \
139 NETIF_MSG_TX_ERR)
140
141/* length of time before we decide the hardware is borked,
142 * and dev->tx_timeout() should be called to fix the problem
143 */
144#define CAS_TX_TIMEOUT (HZ)
145#define CAS_LINK_TIMEOUT (22*HZ/10)
146#define CAS_LINK_FAST_TIMEOUT (1)
147
148/* timeout values for state changing. these specify the number
149 * of 10us delays to be used before giving up.
150 */
151#define STOP_TRIES_PHY 1000
152#define STOP_TRIES 5000
153
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400154/* specify a minimum frame size to deal with some fifo issues
David S. Miller1f26dac2005-09-27 15:24:13 -0700155 * max mtu == 2 * page size - ethernet header - 64 - swivel =
156 * 2 * page_size - 0x50
157 */
158#define CAS_MIN_FRAME 97
159#define CAS_1000MB_MIN_FRAME 255
160#define CAS_MIN_MTU 60
161#define CAS_MAX_MTU min(((cp->page_size << 1) - 0x50), 9000)
162
163#if 1
164/*
165 * Eliminate these and use separate atomic counters for each, to
166 * avoid a race condition.
167 */
168#else
169#define CAS_RESET_MTU 1
170#define CAS_RESET_ALL 2
171#define CAS_RESET_SPARE 3
172#endif
173
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500174static char version[] =
David S. Miller1f26dac2005-09-27 15:24:13 -0700175 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
176
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800177static int cassini_debug = -1; /* -1 == use CAS_DEF_MSG_ENABLE as value */
178static int link_mode;
179
David S. Miller1f26dac2005-09-27 15:24:13 -0700180MODULE_AUTHOR("Adrian Sun (asun@darksunrising.com)");
181MODULE_DESCRIPTION("Sun Cassini(+) ethernet driver");
182MODULE_LICENSE("GPL");
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700183MODULE_FIRMWARE("sun/cassini.bin");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800184module_param(cassini_debug, int, 0);
David S. Miller1f26dac2005-09-27 15:24:13 -0700185MODULE_PARM_DESC(cassini_debug, "Cassini bitmapped debugging message enable value");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800186module_param(link_mode, int, 0);
David S. Miller1f26dac2005-09-27 15:24:13 -0700187MODULE_PARM_DESC(link_mode, "default link mode");
188
189/*
190 * Work around for a PCS bug in which the link goes down due to the chip
191 * being confused and never showing a link status of "up."
192 */
193#define DEFAULT_LINKDOWN_TIMEOUT 5
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400194/*
David S. Miller1f26dac2005-09-27 15:24:13 -0700195 * Value in seconds, for user input.
196 */
197static int linkdown_timeout = DEFAULT_LINKDOWN_TIMEOUT;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800198module_param(linkdown_timeout, int, 0);
David S. Miller1f26dac2005-09-27 15:24:13 -0700199MODULE_PARM_DESC(linkdown_timeout,
200"min reset interval in sec. for PCS linkdown issue; disabled if not positive");
201
202/*
203 * value in 'ticks' (units used by jiffies). Set when we init the
204 * module because 'HZ' in actually a function call on some flavors of
205 * Linux. This will default to DEFAULT_LINKDOWN_TIMEOUT * HZ.
206 */
207static int link_transition_timeout;
208
209
David S. Miller1f26dac2005-09-27 15:24:13 -0700210
Bill Pembertonf73d12b2012-12-03 09:24:02 -0500211static u16 link_modes[] = {
David S. Miller1f26dac2005-09-27 15:24:13 -0700212 BMCR_ANENABLE, /* 0 : autoneg */
213 0, /* 1 : 10bt half duplex */
214 BMCR_SPEED100, /* 2 : 100bt half duplex */
215 BMCR_FULLDPLX, /* 3 : 10bt full duplex */
216 BMCR_SPEED100|BMCR_FULLDPLX, /* 4 : 100bt full duplex */
217 CAS_BMCR_SPEED1000|BMCR_FULLDPLX /* 5 : 1000bt full duplex */
218};
219
Benoit Taine9baa3c32014-08-08 15:56:03 +0200220static const struct pci_device_id cas_pci_tbl[] = {
David S. Miller1f26dac2005-09-27 15:24:13 -0700221 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_CASSINI,
222 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
223 { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SATURN,
224 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
225 { 0, }
226};
227
228MODULE_DEVICE_TABLE(pci, cas_pci_tbl);
229
230static void cas_set_link_modes(struct cas *cp);
231
232static inline void cas_lock_tx(struct cas *cp)
233{
234 int i;
235
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400236 for (i = 0; i < N_TX_RINGS; i++)
Emil Goodea9de0502014-05-09 01:07:17 +0200237 spin_lock_nested(&cp->tx_lock[i], i);
David S. Miller1f26dac2005-09-27 15:24:13 -0700238}
239
240static inline void cas_lock_all(struct cas *cp)
241{
242 spin_lock_irq(&cp->lock);
243 cas_lock_tx(cp);
244}
245
246/* WTZ: QA was finding deadlock problems with the previous
247 * versions after long test runs with multiple cards per machine.
248 * See if replacing cas_lock_all with safer versions helps. The
249 * symptoms QA is reporting match those we'd expect if interrupts
250 * aren't being properly restored, and we fixed a previous deadlock
251 * with similar symptoms by using save/restore versions in other
252 * places.
253 */
254#define cas_lock_all_save(cp, flags) \
255do { \
256 struct cas *xxxcp = (cp); \
257 spin_lock_irqsave(&xxxcp->lock, flags); \
258 cas_lock_tx(xxxcp); \
259} while (0)
260
261static inline void cas_unlock_tx(struct cas *cp)
262{
263 int i;
264
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400265 for (i = N_TX_RINGS; i > 0; i--)
266 spin_unlock(&cp->tx_lock[i - 1]);
David S. Miller1f26dac2005-09-27 15:24:13 -0700267}
268
269static inline void cas_unlock_all(struct cas *cp)
270{
271 cas_unlock_tx(cp);
272 spin_unlock_irq(&cp->lock);
273}
274
275#define cas_unlock_all_restore(cp, flags) \
276do { \
277 struct cas *xxxcp = (cp); \
278 cas_unlock_tx(xxxcp); \
279 spin_unlock_irqrestore(&xxxcp->lock, flags); \
280} while (0)
281
282static void cas_disable_irq(struct cas *cp, const int ring)
283{
284 /* Make sure we won't get any more interrupts */
285 if (ring == 0) {
286 writel(0xFFFFFFFF, cp->regs + REG_INTR_MASK);
287 return;
288 }
289
290 /* disable completion interrupts and selectively mask */
291 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
292 switch (ring) {
293#if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
294#ifdef USE_PCI_INTB
295 case 1:
296#endif
297#ifdef USE_PCI_INTC
298 case 2:
299#endif
300#ifdef USE_PCI_INTD
301 case 3:
302#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400303 writel(INTRN_MASK_CLEAR_ALL | INTRN_MASK_RX_EN,
David S. Miller1f26dac2005-09-27 15:24:13 -0700304 cp->regs + REG_PLUS_INTRN_MASK(ring));
305 break;
306#endif
307 default:
308 writel(INTRN_MASK_CLEAR_ALL, cp->regs +
309 REG_PLUS_INTRN_MASK(ring));
310 break;
311 }
312 }
313}
314
315static inline void cas_mask_intr(struct cas *cp)
316{
317 int i;
318
319 for (i = 0; i < N_RX_COMP_RINGS; i++)
320 cas_disable_irq(cp, i);
321}
322
323static void cas_enable_irq(struct cas *cp, const int ring)
324{
325 if (ring == 0) { /* all but TX_DONE */
326 writel(INTR_TX_DONE, cp->regs + REG_INTR_MASK);
327 return;
328 }
329
330 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
331 switch (ring) {
332#if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
333#ifdef USE_PCI_INTB
334 case 1:
335#endif
336#ifdef USE_PCI_INTC
337 case 2:
338#endif
339#ifdef USE_PCI_INTD
340 case 3:
341#endif
342 writel(INTRN_MASK_RX_EN, cp->regs +
343 REG_PLUS_INTRN_MASK(ring));
344 break;
345#endif
346 default:
347 break;
348 }
349 }
350}
351
352static inline void cas_unmask_intr(struct cas *cp)
353{
354 int i;
355
356 for (i = 0; i < N_RX_COMP_RINGS; i++)
357 cas_enable_irq(cp, i);
358}
359
360static inline void cas_entropy_gather(struct cas *cp)
361{
362#ifdef USE_ENTROPY_DEV
363 if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
364 return;
365
366 batch_entropy_store(readl(cp->regs + REG_ENTROPY_IV),
367 readl(cp->regs + REG_ENTROPY_IV),
368 sizeof(uint64_t)*8);
369#endif
370}
371
372static inline void cas_entropy_reset(struct cas *cp)
373{
374#ifdef USE_ENTROPY_DEV
375 if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
376 return;
377
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400378 writel(BIM_LOCAL_DEV_PAD | BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_EXT,
David S. Miller1f26dac2005-09-27 15:24:13 -0700379 cp->regs + REG_BIM_LOCAL_DEV_EN);
380 writeb(ENTROPY_RESET_STC_MODE, cp->regs + REG_ENTROPY_RESET);
381 writeb(0x55, cp->regs + REG_ENTROPY_RAND_REG);
382
383 /* if we read back 0x0, we don't have an entropy device */
384 if (readb(cp->regs + REG_ENTROPY_RAND_REG) == 0)
385 cp->cas_flags &= ~CAS_FLAG_ENTROPY_DEV;
386#endif
387}
388
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400389/* access to the phy. the following assumes that we've initialized the MIF to
David S. Miller1f26dac2005-09-27 15:24:13 -0700390 * be in frame rather than bit-bang mode
391 */
392static u16 cas_phy_read(struct cas *cp, int reg)
393{
394 u32 cmd;
395 int limit = STOP_TRIES_PHY;
396
397 cmd = MIF_FRAME_ST | MIF_FRAME_OP_READ;
398 cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
399 cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
400 cmd |= MIF_FRAME_TURN_AROUND_MSB;
401 writel(cmd, cp->regs + REG_MIF_FRAME);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400402
David S. Miller1f26dac2005-09-27 15:24:13 -0700403 /* poll for completion */
404 while (limit-- > 0) {
405 udelay(10);
406 cmd = readl(cp->regs + REG_MIF_FRAME);
407 if (cmd & MIF_FRAME_TURN_AROUND_LSB)
Eric Dumazet807540b2010-09-23 05:40:09 +0000408 return cmd & MIF_FRAME_DATA_MASK;
David S. Miller1f26dac2005-09-27 15:24:13 -0700409 }
410 return 0xFFFF; /* -1 */
411}
412
413static int cas_phy_write(struct cas *cp, int reg, u16 val)
414{
415 int limit = STOP_TRIES_PHY;
416 u32 cmd;
417
418 cmd = MIF_FRAME_ST | MIF_FRAME_OP_WRITE;
419 cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
420 cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
421 cmd |= MIF_FRAME_TURN_AROUND_MSB;
422 cmd |= val & MIF_FRAME_DATA_MASK;
423 writel(cmd, cp->regs + REG_MIF_FRAME);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400424
David S. Miller1f26dac2005-09-27 15:24:13 -0700425 /* poll for completion */
426 while (limit-- > 0) {
427 udelay(10);
428 cmd = readl(cp->regs + REG_MIF_FRAME);
429 if (cmd & MIF_FRAME_TURN_AROUND_LSB)
430 return 0;
431 }
432 return -1;
433}
434
435static void cas_phy_powerup(struct cas *cp)
436{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400437 u16 ctl = cas_phy_read(cp, MII_BMCR);
David S. Miller1f26dac2005-09-27 15:24:13 -0700438
439 if ((ctl & BMCR_PDOWN) == 0)
440 return;
441 ctl &= ~BMCR_PDOWN;
442 cas_phy_write(cp, MII_BMCR, ctl);
443}
444
445static void cas_phy_powerdown(struct cas *cp)
446{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400447 u16 ctl = cas_phy_read(cp, MII_BMCR);
David S. Miller1f26dac2005-09-27 15:24:13 -0700448
449 if (ctl & BMCR_PDOWN)
450 return;
451 ctl |= BMCR_PDOWN;
452 cas_phy_write(cp, MII_BMCR, ctl);
453}
454
455/* cp->lock held. note: the last put_page will free the buffer */
456static int cas_page_free(struct cas *cp, cas_page_t *page)
457{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400458 pci_unmap_page(cp->pdev, page->dma_addr, cp->page_size,
David S. Miller1f26dac2005-09-27 15:24:13 -0700459 PCI_DMA_FROMDEVICE);
460 __free_pages(page->buffer, cp->page_order);
461 kfree(page);
462 return 0;
463}
464
465#ifdef RX_COUNT_BUFFERS
466#define RX_USED_ADD(x, y) ((x)->used += (y))
467#define RX_USED_SET(x, y) ((x)->used = (y))
468#else
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400469#define RX_USED_ADD(x, y)
David S. Miller1f26dac2005-09-27 15:24:13 -0700470#define RX_USED_SET(x, y)
471#endif
472
473/* local page allocation routines for the receive buffers. jumbo pages
474 * require at least 8K contiguous and 8K aligned buffers.
475 */
Al Viro9e249742005-10-21 03:22:29 -0400476static cas_page_t *cas_page_alloc(struct cas *cp, const gfp_t flags)
David S. Miller1f26dac2005-09-27 15:24:13 -0700477{
478 cas_page_t *page;
479
480 page = kmalloc(sizeof(cas_page_t), flags);
481 if (!page)
482 return NULL;
483
484 INIT_LIST_HEAD(&page->list);
485 RX_USED_SET(page, 0);
486 page->buffer = alloc_pages(flags, cp->page_order);
487 if (!page->buffer)
488 goto page_err;
489 page->dma_addr = pci_map_page(cp->pdev, page->buffer, 0,
490 cp->page_size, PCI_DMA_FROMDEVICE);
491 return page;
492
493page_err:
494 kfree(page);
495 return NULL;
496}
497
498/* initialize spare pool of rx buffers, but allocate during the open */
499static void cas_spare_init(struct cas *cp)
500{
501 spin_lock(&cp->rx_inuse_lock);
502 INIT_LIST_HEAD(&cp->rx_inuse_list);
503 spin_unlock(&cp->rx_inuse_lock);
504
505 spin_lock(&cp->rx_spare_lock);
506 INIT_LIST_HEAD(&cp->rx_spare_list);
507 cp->rx_spares_needed = RX_SPARE_COUNT;
508 spin_unlock(&cp->rx_spare_lock);
509}
510
511/* used on close. free all the spare buffers. */
512static void cas_spare_free(struct cas *cp)
513{
514 struct list_head list, *elem, *tmp;
515
516 /* free spare buffers */
517 INIT_LIST_HEAD(&list);
518 spin_lock(&cp->rx_spare_lock);
Robert P. J. Day9bd512f2008-03-23 22:47:53 -0700519 list_splice_init(&cp->rx_spare_list, &list);
David S. Miller1f26dac2005-09-27 15:24:13 -0700520 spin_unlock(&cp->rx_spare_lock);
521 list_for_each_safe(elem, tmp, &list) {
522 cas_page_free(cp, list_entry(elem, cas_page_t, list));
523 }
524
525 INIT_LIST_HEAD(&list);
526#if 1
527 /*
528 * Looks like Adrian had protected this with a different
529 * lock than used everywhere else to manipulate this list.
530 */
531 spin_lock(&cp->rx_inuse_lock);
Robert P. J. Day9bd512f2008-03-23 22:47:53 -0700532 list_splice_init(&cp->rx_inuse_list, &list);
David S. Miller1f26dac2005-09-27 15:24:13 -0700533 spin_unlock(&cp->rx_inuse_lock);
534#else
535 spin_lock(&cp->rx_spare_lock);
Robert P. J. Day9bd512f2008-03-23 22:47:53 -0700536 list_splice_init(&cp->rx_inuse_list, &list);
David S. Miller1f26dac2005-09-27 15:24:13 -0700537 spin_unlock(&cp->rx_spare_lock);
538#endif
539 list_for_each_safe(elem, tmp, &list) {
540 cas_page_free(cp, list_entry(elem, cas_page_t, list));
541 }
542}
543
544/* replenish spares if needed */
Al Viro9e249742005-10-21 03:22:29 -0400545static void cas_spare_recover(struct cas *cp, const gfp_t flags)
David S. Miller1f26dac2005-09-27 15:24:13 -0700546{
547 struct list_head list, *elem, *tmp;
548 int needed, i;
549
550 /* check inuse list. if we don't need any more free buffers,
551 * just free it
552 */
553
554 /* make a local copy of the list */
555 INIT_LIST_HEAD(&list);
556 spin_lock(&cp->rx_inuse_lock);
Robert P. J. Day9bd512f2008-03-23 22:47:53 -0700557 list_splice_init(&cp->rx_inuse_list, &list);
David S. Miller1f26dac2005-09-27 15:24:13 -0700558 spin_unlock(&cp->rx_inuse_lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400559
David S. Miller1f26dac2005-09-27 15:24:13 -0700560 list_for_each_safe(elem, tmp, &list) {
561 cas_page_t *page = list_entry(elem, cas_page_t, list);
562
Nick Piggine2867812008-07-25 19:45:30 -0700563 /*
564 * With the lockless pagecache, cassini buffering scheme gets
565 * slightly less accurate: we might find that a page has an
566 * elevated reference count here, due to a speculative ref,
567 * and skip it as in-use. Ideally we would be able to reclaim
568 * it. However this would be such a rare case, it doesn't
569 * matter too much as we should pick it up the next time round.
570 *
571 * Importantly, if we find that the page has a refcount of 1
572 * here (our refcount), then we know it is definitely not inuse
573 * so we can reuse it.
574 */
David S. Miller9de4dfb42008-01-03 19:33:50 -0800575 if (page_count(page->buffer) > 1)
David S. Miller1f26dac2005-09-27 15:24:13 -0700576 continue;
577
578 list_del(elem);
579 spin_lock(&cp->rx_spare_lock);
580 if (cp->rx_spares_needed > 0) {
581 list_add(elem, &cp->rx_spare_list);
582 cp->rx_spares_needed--;
583 spin_unlock(&cp->rx_spare_lock);
584 } else {
585 spin_unlock(&cp->rx_spare_lock);
586 cas_page_free(cp, page);
587 }
588 }
589
590 /* put any inuse buffers back on the list */
591 if (!list_empty(&list)) {
592 spin_lock(&cp->rx_inuse_lock);
593 list_splice(&list, &cp->rx_inuse_list);
594 spin_unlock(&cp->rx_inuse_lock);
595 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400596
David S. Miller1f26dac2005-09-27 15:24:13 -0700597 spin_lock(&cp->rx_spare_lock);
598 needed = cp->rx_spares_needed;
599 spin_unlock(&cp->rx_spare_lock);
600 if (!needed)
601 return;
602
603 /* we still need spares, so try to allocate some */
604 INIT_LIST_HEAD(&list);
605 i = 0;
606 while (i < needed) {
607 cas_page_t *spare = cas_page_alloc(cp, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400608 if (!spare)
David S. Miller1f26dac2005-09-27 15:24:13 -0700609 break;
610 list_add(&spare->list, &list);
611 i++;
612 }
613
614 spin_lock(&cp->rx_spare_lock);
615 list_splice(&list, &cp->rx_spare_list);
616 cp->rx_spares_needed -= i;
617 spin_unlock(&cp->rx_spare_lock);
618}
619
620/* pull a page from the list. */
621static cas_page_t *cas_page_dequeue(struct cas *cp)
622{
623 struct list_head *entry;
624 int recover;
625
626 spin_lock(&cp->rx_spare_lock);
627 if (list_empty(&cp->rx_spare_list)) {
628 /* try to do a quick recovery */
629 spin_unlock(&cp->rx_spare_lock);
630 cas_spare_recover(cp, GFP_ATOMIC);
631 spin_lock(&cp->rx_spare_lock);
632 if (list_empty(&cp->rx_spare_list)) {
Joe Perches436d27d2010-02-17 15:01:53 +0000633 netif_err(cp, rx_err, cp->dev,
634 "no spare buffers available\n");
David S. Miller1f26dac2005-09-27 15:24:13 -0700635 spin_unlock(&cp->rx_spare_lock);
636 return NULL;
637 }
638 }
639
640 entry = cp->rx_spare_list.next;
641 list_del(entry);
642 recover = ++cp->rx_spares_needed;
643 spin_unlock(&cp->rx_spare_lock);
644
645 /* trigger the timer to do the recovery */
646 if ((recover & (RX_SPARE_RECOVER_VAL - 1)) == 0) {
647#if 1
648 atomic_inc(&cp->reset_task_pending);
649 atomic_inc(&cp->reset_task_pending_spare);
650 schedule_work(&cp->reset_task);
651#else
652 atomic_set(&cp->reset_task_pending, CAS_RESET_SPARE);
653 schedule_work(&cp->reset_task);
654#endif
655 }
656 return list_entry(entry, cas_page_t, list);
657}
658
659
660static void cas_mif_poll(struct cas *cp, const int enable)
661{
662 u32 cfg;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400663
664 cfg = readl(cp->regs + REG_MIF_CFG);
David S. Miller1f26dac2005-09-27 15:24:13 -0700665 cfg &= (MIF_CFG_MDIO_0 | MIF_CFG_MDIO_1);
666
667 if (cp->phy_type & CAS_PHY_MII_MDIO1)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400668 cfg |= MIF_CFG_PHY_SELECT;
David S. Miller1f26dac2005-09-27 15:24:13 -0700669
670 /* poll and interrupt on link status change. */
671 if (enable) {
672 cfg |= MIF_CFG_POLL_EN;
673 cfg |= CAS_BASE(MIF_CFG_POLL_REG, MII_BMSR);
674 cfg |= CAS_BASE(MIF_CFG_POLL_PHY, cp->phy_addr);
675 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400676 writel((enable) ? ~(BMSR_LSTATUS | BMSR_ANEGCOMPLETE) : 0xFFFF,
677 cp->regs + REG_MIF_MASK);
David S. Miller1f26dac2005-09-27 15:24:13 -0700678 writel(cfg, cp->regs + REG_MIF_CFG);
679}
680
681/* Must be invoked under cp->lock */
Philippe Reynes2c784b02017-03-04 16:16:12 +0100682static void cas_begin_auto_negotiation(struct cas *cp,
683 const struct ethtool_link_ksettings *ep)
David S. Miller1f26dac2005-09-27 15:24:13 -0700684{
685 u16 ctl;
686#if 1
687 int lcntl;
688 int changed = 0;
689 int oldstate = cp->lstate;
690 int link_was_not_down = !(oldstate == link_down);
691#endif
692 /* Setup link parameters */
693 if (!ep)
694 goto start_aneg;
695 lcntl = cp->link_cntl;
Philippe Reynes2c784b02017-03-04 16:16:12 +0100696 if (ep->base.autoneg == AUTONEG_ENABLE) {
David S. Miller1f26dac2005-09-27 15:24:13 -0700697 cp->link_cntl = BMCR_ANENABLE;
Philippe Reynes2c784b02017-03-04 16:16:12 +0100698 } else {
699 u32 speed = ep->base.speed;
David S. Miller1f26dac2005-09-27 15:24:13 -0700700 cp->link_cntl = 0;
David Decotigny25db0332011-04-27 18:32:39 +0000701 if (speed == SPEED_100)
David S. Miller1f26dac2005-09-27 15:24:13 -0700702 cp->link_cntl |= BMCR_SPEED100;
David Decotigny25db0332011-04-27 18:32:39 +0000703 else if (speed == SPEED_1000)
David S. Miller1f26dac2005-09-27 15:24:13 -0700704 cp->link_cntl |= CAS_BMCR_SPEED1000;
Philippe Reynes2c784b02017-03-04 16:16:12 +0100705 if (ep->base.duplex == DUPLEX_FULL)
David S. Miller1f26dac2005-09-27 15:24:13 -0700706 cp->link_cntl |= BMCR_FULLDPLX;
707 }
708#if 1
709 changed = (lcntl != cp->link_cntl);
710#endif
711start_aneg:
712 if (cp->lstate == link_up) {
Joe Perches436d27d2010-02-17 15:01:53 +0000713 netdev_info(cp->dev, "PCS link down\n");
David S. Miller1f26dac2005-09-27 15:24:13 -0700714 } else {
715 if (changed) {
Joe Perches436d27d2010-02-17 15:01:53 +0000716 netdev_info(cp->dev, "link configuration changed\n");
David S. Miller1f26dac2005-09-27 15:24:13 -0700717 }
718 }
719 cp->lstate = link_down;
720 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
721 if (!cp->hw_running)
722 return;
723#if 1
724 /*
725 * WTZ: If the old state was link_up, we turn off the carrier
726 * to replicate everything we do elsewhere on a link-down
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400727 * event when we were already in a link-up state..
David S. Miller1f26dac2005-09-27 15:24:13 -0700728 */
729 if (oldstate == link_up)
730 netif_carrier_off(cp->dev);
731 if (changed && link_was_not_down) {
732 /*
733 * WTZ: This branch will simply schedule a full reset after
734 * we explicitly changed link modes in an ioctl. See if this
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400735 * fixes the link-problems we were having for forced mode.
David S. Miller1f26dac2005-09-27 15:24:13 -0700736 */
737 atomic_inc(&cp->reset_task_pending);
738 atomic_inc(&cp->reset_task_pending_all);
739 schedule_work(&cp->reset_task);
740 cp->timer_ticks = 0;
741 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
742 return;
743 }
744#endif
745 if (cp->phy_type & CAS_PHY_SERDES) {
746 u32 val = readl(cp->regs + REG_PCS_MII_CTRL);
747
748 if (cp->link_cntl & BMCR_ANENABLE) {
749 val |= (PCS_MII_RESTART_AUTONEG | PCS_MII_AUTONEG_EN);
750 cp->lstate = link_aneg;
751 } else {
752 if (cp->link_cntl & BMCR_FULLDPLX)
753 val |= PCS_MII_CTRL_DUPLEX;
754 val &= ~PCS_MII_AUTONEG_EN;
755 cp->lstate = link_force_ok;
756 }
757 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
758 writel(val, cp->regs + REG_PCS_MII_CTRL);
759
760 } else {
761 cas_mif_poll(cp, 0);
762 ctl = cas_phy_read(cp, MII_BMCR);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400763 ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
David S. Miller1f26dac2005-09-27 15:24:13 -0700764 CAS_BMCR_SPEED1000 | BMCR_ANENABLE);
765 ctl |= cp->link_cntl;
766 if (ctl & BMCR_ANENABLE) {
767 ctl |= BMCR_ANRESTART;
768 cp->lstate = link_aneg;
769 } else {
770 cp->lstate = link_force_ok;
771 }
772 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
773 cas_phy_write(cp, MII_BMCR, ctl);
774 cas_mif_poll(cp, 1);
775 }
776
777 cp->timer_ticks = 0;
778 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
779}
780
781/* Must be invoked under cp->lock. */
782static int cas_reset_mii_phy(struct cas *cp)
783{
784 int limit = STOP_TRIES_PHY;
785 u16 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400786
David S. Miller1f26dac2005-09-27 15:24:13 -0700787 cas_phy_write(cp, MII_BMCR, BMCR_RESET);
788 udelay(100);
Roel Kluinff01b912009-02-02 23:19:50 -0800789 while (--limit) {
David S. Miller1f26dac2005-09-27 15:24:13 -0700790 val = cas_phy_read(cp, MII_BMCR);
791 if ((val & BMCR_RESET) == 0)
792 break;
793 udelay(10);
794 }
Eric Dumazet807540b2010-09-23 05:40:09 +0000795 return limit <= 0;
David S. Miller1f26dac2005-09-27 15:24:13 -0700796}
797
Ben Hutchings15627e82013-07-01 00:13:27 +0100798static void cas_saturn_firmware_init(struct cas *cp)
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700799{
800 const struct firmware *fw;
801 const char fw_name[] = "sun/cassini.bin";
802 int err;
803
804 if (PHY_NS_DP83065 != cp->phy_id)
Ben Hutchings15627e82013-07-01 00:13:27 +0100805 return;
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700806
807 err = request_firmware(&fw, fw_name, &cp->pdev->dev);
808 if (err) {
Joe Perches436d27d2010-02-17 15:01:53 +0000809 pr_err("Failed to load firmware \"%s\"\n",
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700810 fw_name);
Ben Hutchings15627e82013-07-01 00:13:27 +0100811 return;
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700812 }
813 if (fw->size < 2) {
Joe Perches436d27d2010-02-17 15:01:53 +0000814 pr_err("bogus length %zu in \"%s\"\n",
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700815 fw->size, fw_name);
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700816 goto out;
817 }
818 cp->fw_load_addr= fw->data[1] << 8 | fw->data[0];
819 cp->fw_size = fw->size - 2;
820 cp->fw_data = vmalloc(cp->fw_size);
Ben Hutchings15627e82013-07-01 00:13:27 +0100821 if (!cp->fw_data)
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700822 goto out;
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700823 memcpy(cp->fw_data, &fw->data[2], cp->fw_size);
824out:
825 release_firmware(fw);
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700826}
827
David S. Miller1f26dac2005-09-27 15:24:13 -0700828static void cas_saturn_firmware_load(struct cas *cp)
829{
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700830 int i;
David S. Miller1f26dac2005-09-27 15:24:13 -0700831
Ben Hutchings15627e82013-07-01 00:13:27 +0100832 if (!cp->fw_data)
833 return;
834
David S. Miller1f26dac2005-09-27 15:24:13 -0700835 cas_phy_powerdown(cp);
836
837 /* expanded memory access mode */
838 cas_phy_write(cp, DP83065_MII_MEM, 0x0);
839
840 /* pointer configuration for new firmware */
841 cas_phy_write(cp, DP83065_MII_REGE, 0x8ff9);
842 cas_phy_write(cp, DP83065_MII_REGD, 0xbd);
843 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffa);
844 cas_phy_write(cp, DP83065_MII_REGD, 0x82);
845 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffb);
846 cas_phy_write(cp, DP83065_MII_REGD, 0x0);
847 cas_phy_write(cp, DP83065_MII_REGE, 0x8ffc);
848 cas_phy_write(cp, DP83065_MII_REGD, 0x39);
849
850 /* download new firmware */
851 cas_phy_write(cp, DP83065_MII_MEM, 0x1);
Jaswinder Singhfcaa4062008-09-22 19:27:10 -0700852 cas_phy_write(cp, DP83065_MII_REGE, cp->fw_load_addr);
853 for (i = 0; i < cp->fw_size; i++)
854 cas_phy_write(cp, DP83065_MII_REGD, cp->fw_data[i]);
David S. Miller1f26dac2005-09-27 15:24:13 -0700855
856 /* enable firmware */
857 cas_phy_write(cp, DP83065_MII_REGE, 0x8ff8);
858 cas_phy_write(cp, DP83065_MII_REGD, 0x1);
859}
860
861
862/* phy initialization */
863static void cas_phy_init(struct cas *cp)
864{
865 u16 val;
866
867 /* if we're in MII/GMII mode, set up phy */
868 if (CAS_PHY_MII(cp->phy_type)) {
869 writel(PCS_DATAPATH_MODE_MII,
870 cp->regs + REG_PCS_DATAPATH_MODE);
871
872 cas_mif_poll(cp, 0);
873 cas_reset_mii_phy(cp); /* take out of isolate mode */
874
875 if (PHY_LUCENT_B0 == cp->phy_id) {
876 /* workaround link up/down issue with lucent */
877 cas_phy_write(cp, LUCENT_MII_REG, 0x8000);
878 cas_phy_write(cp, MII_BMCR, 0x00f1);
879 cas_phy_write(cp, LUCENT_MII_REG, 0x0);
880
881 } else if (PHY_BROADCOM_B0 == (cp->phy_id & 0xFFFFFFFC)) {
882 /* workarounds for broadcom phy */
883 cas_phy_write(cp, BROADCOM_MII_REG8, 0x0C20);
884 cas_phy_write(cp, BROADCOM_MII_REG7, 0x0012);
885 cas_phy_write(cp, BROADCOM_MII_REG5, 0x1804);
886 cas_phy_write(cp, BROADCOM_MII_REG7, 0x0013);
887 cas_phy_write(cp, BROADCOM_MII_REG5, 0x1204);
888 cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
889 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0132);
890 cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
891 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0232);
892 cas_phy_write(cp, BROADCOM_MII_REG7, 0x201F);
893 cas_phy_write(cp, BROADCOM_MII_REG5, 0x0A20);
894
895 } else if (PHY_BROADCOM_5411 == cp->phy_id) {
896 val = cas_phy_read(cp, BROADCOM_MII_REG4);
897 val = cas_phy_read(cp, BROADCOM_MII_REG4);
898 if (val & 0x0080) {
899 /* link workaround */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400900 cas_phy_write(cp, BROADCOM_MII_REG4,
David S. Miller1f26dac2005-09-27 15:24:13 -0700901 val & ~0x0080);
902 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400903
David S. Miller1f26dac2005-09-27 15:24:13 -0700904 } else if (cp->cas_flags & CAS_FLAG_SATURN) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400905 writel((cp->phy_type & CAS_PHY_MII_MDIO0) ?
906 SATURN_PCFG_FSI : 0x0,
David S. Miller1f26dac2005-09-27 15:24:13 -0700907 cp->regs + REG_SATURN_PCFG);
908
909 /* load firmware to address 10Mbps auto-negotiation
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400910 * issue. NOTE: this will need to be changed if the
David S. Miller1f26dac2005-09-27 15:24:13 -0700911 * default firmware gets fixed.
912 */
913 if (PHY_NS_DP83065 == cp->phy_id) {
914 cas_saturn_firmware_load(cp);
915 }
916 cas_phy_powerup(cp);
917 }
918
919 /* advertise capabilities */
920 val = cas_phy_read(cp, MII_BMCR);
921 val &= ~BMCR_ANENABLE;
922 cas_phy_write(cp, MII_BMCR, val);
923 udelay(10);
924
925 cas_phy_write(cp, MII_ADVERTISE,
926 cas_phy_read(cp, MII_ADVERTISE) |
927 (ADVERTISE_10HALF | ADVERTISE_10FULL |
928 ADVERTISE_100HALF | ADVERTISE_100FULL |
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400929 CAS_ADVERTISE_PAUSE |
David S. Miller1f26dac2005-09-27 15:24:13 -0700930 CAS_ADVERTISE_ASYM_PAUSE));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400931
David S. Miller1f26dac2005-09-27 15:24:13 -0700932 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
933 /* make sure that we don't advertise half
934 * duplex to avoid a chip issue
935 */
936 val = cas_phy_read(cp, CAS_MII_1000_CTRL);
937 val &= ~CAS_ADVERTISE_1000HALF;
938 val |= CAS_ADVERTISE_1000FULL;
939 cas_phy_write(cp, CAS_MII_1000_CTRL, val);
940 }
941
942 } else {
943 /* reset pcs for serdes */
944 u32 val;
945 int limit;
946
947 writel(PCS_DATAPATH_MODE_SERDES,
948 cp->regs + REG_PCS_DATAPATH_MODE);
949
950 /* enable serdes pins on saturn */
951 if (cp->cas_flags & CAS_FLAG_SATURN)
952 writel(0, cp->regs + REG_SATURN_PCFG);
953
954 /* Reset PCS unit. */
955 val = readl(cp->regs + REG_PCS_MII_CTRL);
956 val |= PCS_MII_RESET;
957 writel(val, cp->regs + REG_PCS_MII_CTRL);
958
959 limit = STOP_TRIES;
Roel Kluinff01b912009-02-02 23:19:50 -0800960 while (--limit > 0) {
David S. Miller1f26dac2005-09-27 15:24:13 -0700961 udelay(10);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400962 if ((readl(cp->regs + REG_PCS_MII_CTRL) &
David S. Miller1f26dac2005-09-27 15:24:13 -0700963 PCS_MII_RESET) == 0)
964 break;
965 }
966 if (limit <= 0)
Joe Perches436d27d2010-02-17 15:01:53 +0000967 netdev_warn(cp->dev, "PCS reset bit would not clear [%08x]\n",
968 readl(cp->regs + REG_PCS_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -0700969
970 /* Make sure PCS is disabled while changing advertisement
971 * configuration.
972 */
973 writel(0x0, cp->regs + REG_PCS_CFG);
974
975 /* Advertise all capabilities except half-duplex. */
976 val = readl(cp->regs + REG_PCS_MII_ADVERT);
977 val &= ~PCS_MII_ADVERT_HD;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400978 val |= (PCS_MII_ADVERT_FD | PCS_MII_ADVERT_SYM_PAUSE |
David S. Miller1f26dac2005-09-27 15:24:13 -0700979 PCS_MII_ADVERT_ASYM_PAUSE);
980 writel(val, cp->regs + REG_PCS_MII_ADVERT);
981
982 /* enable PCS */
983 writel(PCS_CFG_EN, cp->regs + REG_PCS_CFG);
984
985 /* pcs workaround: enable sync detect */
986 writel(PCS_SERDES_CTRL_SYNCD_EN,
987 cp->regs + REG_PCS_SERDES_CTRL);
988 }
989}
990
991
992static int cas_pcs_link_check(struct cas *cp)
993{
994 u32 stat, state_machine;
995 int retval = 0;
996
997 /* The link status bit latches on zero, so you must
998 * read it twice in such a case to see a transition
999 * to the link being up.
1000 */
1001 stat = readl(cp->regs + REG_PCS_MII_STATUS);
1002 if ((stat & PCS_MII_STATUS_LINK_STATUS) == 0)
1003 stat = readl(cp->regs + REG_PCS_MII_STATUS);
1004
1005 /* The remote-fault indication is only valid
1006 * when autoneg has completed.
1007 */
1008 if ((stat & (PCS_MII_STATUS_AUTONEG_COMP |
1009 PCS_MII_STATUS_REMOTE_FAULT)) ==
Joe Perches436d27d2010-02-17 15:01:53 +00001010 (PCS_MII_STATUS_AUTONEG_COMP | PCS_MII_STATUS_REMOTE_FAULT))
1011 netif_info(cp, link, cp->dev, "PCS RemoteFault\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001012
1013 /* work around link detection issue by querying the PCS state
1014 * machine directly.
1015 */
1016 state_machine = readl(cp->regs + REG_PCS_STATE_MACHINE);
1017 if ((state_machine & PCS_SM_LINK_STATE_MASK) != SM_LINK_STATE_UP) {
1018 stat &= ~PCS_MII_STATUS_LINK_STATUS;
1019 } else if (state_machine & PCS_SM_WORD_SYNC_STATE_MASK) {
1020 stat |= PCS_MII_STATUS_LINK_STATUS;
1021 }
1022
1023 if (stat & PCS_MII_STATUS_LINK_STATUS) {
1024 if (cp->lstate != link_up) {
1025 if (cp->opened) {
1026 cp->lstate = link_up;
1027 cp->link_transition = LINK_TRANSITION_LINK_UP;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001028
David S. Miller1f26dac2005-09-27 15:24:13 -07001029 cas_set_link_modes(cp);
1030 netif_carrier_on(cp->dev);
1031 }
1032 }
1033 } else if (cp->lstate == link_up) {
1034 cp->lstate = link_down;
1035 if (link_transition_timeout != 0 &&
1036 cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1037 !cp->link_transition_jiffies_valid) {
1038 /*
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001039 * force a reset, as a workaround for the
1040 * link-failure problem. May want to move this to a
David S. Miller1f26dac2005-09-27 15:24:13 -07001041 * point a bit earlier in the sequence. If we had
1042 * generated a reset a short time ago, we'll wait for
1043 * the link timer to check the status until a
1044 * timer expires (link_transistion_jiffies_valid is
1045 * true when the timer is running.) Instead of using
1046 * a system timer, we just do a check whenever the
1047 * link timer is running - this clears the flag after
1048 * a suitable delay.
1049 */
1050 retval = 1;
1051 cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1052 cp->link_transition_jiffies = jiffies;
1053 cp->link_transition_jiffies_valid = 1;
1054 } else {
1055 cp->link_transition = LINK_TRANSITION_ON_FAILURE;
1056 }
1057 netif_carrier_off(cp->dev);
Joe Perches436d27d2010-02-17 15:01:53 +00001058 if (cp->opened)
1059 netif_info(cp, link, cp->dev, "PCS link down\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001060
1061 /* Cassini only: if you force a mode, there can be
1062 * sync problems on link down. to fix that, the following
1063 * things need to be checked:
1064 * 1) read serialink state register
1065 * 2) read pcs status register to verify link down.
1066 * 3) if link down and serial link == 0x03, then you need
1067 * to global reset the chip.
1068 */
1069 if ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0) {
1070 /* should check to see if we're in a forced mode */
1071 stat = readl(cp->regs + REG_PCS_SERDES_STATE);
1072 if (stat == 0x03)
1073 return 1;
1074 }
1075 } else if (cp->lstate == link_down) {
1076 if (link_transition_timeout != 0 &&
1077 cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
1078 !cp->link_transition_jiffies_valid) {
1079 /* force a reset, as a workaround for the
1080 * link-failure problem. May want to move
1081 * this to a point a bit earlier in the
1082 * sequence.
1083 */
1084 retval = 1;
1085 cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
1086 cp->link_transition_jiffies = jiffies;
1087 cp->link_transition_jiffies_valid = 1;
1088 } else {
1089 cp->link_transition = LINK_TRANSITION_STILL_FAILED;
1090 }
1091 }
1092
1093 return retval;
1094}
1095
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001096static int cas_pcs_interrupt(struct net_device *dev,
David S. Miller1f26dac2005-09-27 15:24:13 -07001097 struct cas *cp, u32 status)
1098{
1099 u32 stat = readl(cp->regs + REG_PCS_INTR_STATUS);
1100
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001101 if ((stat & PCS_INTR_STATUS_LINK_CHANGE) == 0)
David S. Miller1f26dac2005-09-27 15:24:13 -07001102 return 0;
1103 return cas_pcs_link_check(cp);
1104}
1105
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001106static int cas_txmac_interrupt(struct net_device *dev,
David S. Miller1f26dac2005-09-27 15:24:13 -07001107 struct cas *cp, u32 status)
1108{
1109 u32 txmac_stat = readl(cp->regs + REG_MAC_TX_STATUS);
1110
1111 if (!txmac_stat)
1112 return 0;
1113
Joe Perches436d27d2010-02-17 15:01:53 +00001114 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1115 "txmac interrupt, txmac_stat: 0x%x\n", txmac_stat);
David S. Miller1f26dac2005-09-27 15:24:13 -07001116
1117 /* Defer timer expiration is quite normal,
1118 * don't even log the event.
1119 */
1120 if ((txmac_stat & MAC_TX_DEFER_TIMER) &&
1121 !(txmac_stat & ~MAC_TX_DEFER_TIMER))
1122 return 0;
1123
1124 spin_lock(&cp->stat_lock[0]);
1125 if (txmac_stat & MAC_TX_UNDERRUN) {
Joe Perches436d27d2010-02-17 15:01:53 +00001126 netdev_err(dev, "TX MAC xmit underrun\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001127 cp->net_stats[0].tx_fifo_errors++;
1128 }
1129
1130 if (txmac_stat & MAC_TX_MAX_PACKET_ERR) {
Joe Perches436d27d2010-02-17 15:01:53 +00001131 netdev_err(dev, "TX MAC max packet size error\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001132 cp->net_stats[0].tx_errors++;
1133 }
1134
1135 /* The rest are all cases of one of the 16-bit TX
1136 * counters expiring.
1137 */
1138 if (txmac_stat & MAC_TX_COLL_NORMAL)
1139 cp->net_stats[0].collisions += 0x10000;
1140
1141 if (txmac_stat & MAC_TX_COLL_EXCESS) {
1142 cp->net_stats[0].tx_aborted_errors += 0x10000;
1143 cp->net_stats[0].collisions += 0x10000;
1144 }
1145
1146 if (txmac_stat & MAC_TX_COLL_LATE) {
1147 cp->net_stats[0].tx_aborted_errors += 0x10000;
1148 cp->net_stats[0].collisions += 0x10000;
1149 }
1150 spin_unlock(&cp->stat_lock[0]);
1151
1152 /* We do not keep track of MAC_TX_COLL_FIRST and
1153 * MAC_TX_PEAK_ATTEMPTS events.
1154 */
1155 return 0;
1156}
1157
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001158static void cas_load_firmware(struct cas *cp, cas_hp_inst_t *firmware)
David S. Miller1f26dac2005-09-27 15:24:13 -07001159{
1160 cas_hp_inst_t *inst;
1161 u32 val;
1162 int i;
1163
1164 i = 0;
1165 while ((inst = firmware) && inst->note) {
1166 writel(i, cp->regs + REG_HP_INSTR_RAM_ADDR);
1167
1168 val = CAS_BASE(HP_INSTR_RAM_HI_VAL, inst->val);
1169 val |= CAS_BASE(HP_INSTR_RAM_HI_MASK, inst->mask);
1170 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_HI);
1171
1172 val = CAS_BASE(HP_INSTR_RAM_MID_OUTARG, inst->outarg >> 10);
1173 val |= CAS_BASE(HP_INSTR_RAM_MID_OUTOP, inst->outop);
1174 val |= CAS_BASE(HP_INSTR_RAM_MID_FNEXT, inst->fnext);
1175 val |= CAS_BASE(HP_INSTR_RAM_MID_FOFF, inst->foff);
1176 val |= CAS_BASE(HP_INSTR_RAM_MID_SNEXT, inst->snext);
1177 val |= CAS_BASE(HP_INSTR_RAM_MID_SOFF, inst->soff);
1178 val |= CAS_BASE(HP_INSTR_RAM_MID_OP, inst->op);
1179 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_MID);
1180
1181 val = CAS_BASE(HP_INSTR_RAM_LOW_OUTMASK, inst->outmask);
1182 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTSHIFT, inst->outshift);
1183 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTEN, inst->outenab);
1184 val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTARG, inst->outarg);
1185 writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_LOW);
1186 ++firmware;
1187 ++i;
1188 }
1189}
1190
1191static void cas_init_rx_dma(struct cas *cp)
1192{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193 u64 desc_dma = cp->block_dvma;
David S. Miller1f26dac2005-09-27 15:24:13 -07001194 u32 val;
1195 int i, size;
1196
1197 /* rx free descriptors */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001198 val = CAS_BASE(RX_CFG_SWIVEL, RX_SWIVEL_OFF_VAL);
David S. Miller1f26dac2005-09-27 15:24:13 -07001199 val |= CAS_BASE(RX_CFG_DESC_RING, RX_DESC_RINGN_INDEX(0));
1200 val |= CAS_BASE(RX_CFG_COMP_RING, RX_COMP_RINGN_INDEX(0));
1201 if ((N_RX_DESC_RINGS > 1) &&
1202 (cp->cas_flags & CAS_FLAG_REG_PLUS)) /* do desc 2 */
1203 val |= CAS_BASE(RX_CFG_DESC_RING1, RX_DESC_RINGN_INDEX(1));
1204 writel(val, cp->regs + REG_RX_CFG);
1205
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001206 val = (unsigned long) cp->init_rxds[0] -
David S. Miller1f26dac2005-09-27 15:24:13 -07001207 (unsigned long) cp->init_block;
1208 writel((desc_dma + val) >> 32, cp->regs + REG_RX_DB_HI);
1209 writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_DB_LOW);
1210 writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
1211
1212 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001213 /* rx desc 2 is for IPSEC packets. however,
David S. Miller1f26dac2005-09-27 15:24:13 -07001214 * we don't it that for that purpose.
1215 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001216 val = (unsigned long) cp->init_rxds[1] -
David S. Miller1f26dac2005-09-27 15:24:13 -07001217 (unsigned long) cp->init_block;
1218 writel((desc_dma + val) >> 32, cp->regs + REG_PLUS_RX_DB1_HI);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001219 writel((desc_dma + val) & 0xffffffff, cp->regs +
David S. Miller1f26dac2005-09-27 15:24:13 -07001220 REG_PLUS_RX_DB1_LOW);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001221 writel(RX_DESC_RINGN_SIZE(1) - 4, cp->regs +
David S. Miller1f26dac2005-09-27 15:24:13 -07001222 REG_PLUS_RX_KICK1);
1223 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001224
David S. Miller1f26dac2005-09-27 15:24:13 -07001225 /* rx completion registers */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001226 val = (unsigned long) cp->init_rxcs[0] -
David S. Miller1f26dac2005-09-27 15:24:13 -07001227 (unsigned long) cp->init_block;
1228 writel((desc_dma + val) >> 32, cp->regs + REG_RX_CB_HI);
1229 writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_CB_LOW);
1230
1231 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1232 /* rx comp 2-4 */
1233 for (i = 1; i < MAX_RX_COMP_RINGS; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001234 val = (unsigned long) cp->init_rxcs[i] -
David S. Miller1f26dac2005-09-27 15:24:13 -07001235 (unsigned long) cp->init_block;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001236 writel((desc_dma + val) >> 32, cp->regs +
David S. Miller1f26dac2005-09-27 15:24:13 -07001237 REG_PLUS_RX_CBN_HI(i));
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001238 writel((desc_dma + val) & 0xffffffff, cp->regs +
David S. Miller1f26dac2005-09-27 15:24:13 -07001239 REG_PLUS_RX_CBN_LOW(i));
1240 }
1241 }
1242
1243 /* read selective clear regs to prevent spurious interrupts
1244 * on reset because complete == kick.
1245 * selective clear set up to prevent interrupts on resets
1246 */
1247 readl(cp->regs + REG_INTR_STATUS_ALIAS);
1248 writel(INTR_RX_DONE | INTR_RX_BUF_UNAVAIL, cp->regs + REG_ALIAS_CLEAR);
1249 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1250 for (i = 1; i < N_RX_COMP_RINGS; i++)
1251 readl(cp->regs + REG_PLUS_INTRN_STATUS_ALIAS(i));
1252
1253 /* 2 is different from 3 and 4 */
1254 if (N_RX_COMP_RINGS > 1)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001255 writel(INTR_RX_DONE_ALT | INTR_RX_BUF_UNAVAIL_1,
David S. Miller1f26dac2005-09-27 15:24:13 -07001256 cp->regs + REG_PLUS_ALIASN_CLEAR(1));
1257
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001258 for (i = 2; i < N_RX_COMP_RINGS; i++)
1259 writel(INTR_RX_DONE_ALT,
David S. Miller1f26dac2005-09-27 15:24:13 -07001260 cp->regs + REG_PLUS_ALIASN_CLEAR(i));
1261 }
1262
1263 /* set up pause thresholds */
1264 val = CAS_BASE(RX_PAUSE_THRESH_OFF,
1265 cp->rx_pause_off / RX_PAUSE_THRESH_QUANTUM);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001266 val |= CAS_BASE(RX_PAUSE_THRESH_ON,
David S. Miller1f26dac2005-09-27 15:24:13 -07001267 cp->rx_pause_on / RX_PAUSE_THRESH_QUANTUM);
1268 writel(val, cp->regs + REG_RX_PAUSE_THRESH);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001269
David S. Miller1f26dac2005-09-27 15:24:13 -07001270 /* zero out dma reassembly buffers */
1271 for (i = 0; i < 64; i++) {
1272 writel(i, cp->regs + REG_RX_TABLE_ADDR);
1273 writel(0x0, cp->regs + REG_RX_TABLE_DATA_LOW);
1274 writel(0x0, cp->regs + REG_RX_TABLE_DATA_MID);
1275 writel(0x0, cp->regs + REG_RX_TABLE_DATA_HI);
1276 }
1277
1278 /* make sure address register is 0 for normal operation */
1279 writel(0x0, cp->regs + REG_RX_CTRL_FIFO_ADDR);
1280 writel(0x0, cp->regs + REG_RX_IPP_FIFO_ADDR);
1281
1282 /* interrupt mitigation */
1283#ifdef USE_RX_BLANK
1284 val = CAS_BASE(RX_BLANK_INTR_TIME, RX_BLANK_INTR_TIME_VAL);
1285 val |= CAS_BASE(RX_BLANK_INTR_PKT, RX_BLANK_INTR_PKT_VAL);
1286 writel(val, cp->regs + REG_RX_BLANK);
1287#else
1288 writel(0x0, cp->regs + REG_RX_BLANK);
1289#endif
1290
1291 /* interrupt generation as a function of low water marks for
1292 * free desc and completion entries. these are used to trigger
1293 * housekeeping for rx descs. we don't use the free interrupt
1294 * as it's not very useful
1295 */
1296 /* val = CAS_BASE(RX_AE_THRESH_FREE, RX_AE_FREEN_VAL(0)); */
1297 val = CAS_BASE(RX_AE_THRESH_COMP, RX_AE_COMP_VAL);
1298 writel(val, cp->regs + REG_RX_AE_THRESH);
1299 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
1300 val = CAS_BASE(RX_AE1_THRESH_FREE, RX_AE_FREEN_VAL(1));
1301 writel(val, cp->regs + REG_PLUS_RX_AE1_THRESH);
1302 }
1303
1304 /* Random early detect registers. useful for congestion avoidance.
1305 * this should be tunable.
1306 */
1307 writel(0x0, cp->regs + REG_RX_RED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001308
David S. Miller1f26dac2005-09-27 15:24:13 -07001309 /* receive page sizes. default == 2K (0x800) */
1310 val = 0;
1311 if (cp->page_size == 0x1000)
1312 val = 0x1;
1313 else if (cp->page_size == 0x2000)
1314 val = 0x2;
1315 else if (cp->page_size == 0x4000)
1316 val = 0x3;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001317
David S. Miller1f26dac2005-09-27 15:24:13 -07001318 /* round mtu + offset. constrain to page size. */
1319 size = cp->dev->mtu + 64;
1320 if (size > cp->page_size)
1321 size = cp->page_size;
1322
1323 if (size <= 0x400)
1324 i = 0x0;
1325 else if (size <= 0x800)
1326 i = 0x1;
1327 else if (size <= 0x1000)
1328 i = 0x2;
1329 else
1330 i = 0x3;
1331
1332 cp->mtu_stride = 1 << (i + 10);
1333 val = CAS_BASE(RX_PAGE_SIZE, val);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001334 val |= CAS_BASE(RX_PAGE_SIZE_MTU_STRIDE, i);
David S. Miller1f26dac2005-09-27 15:24:13 -07001335 val |= CAS_BASE(RX_PAGE_SIZE_MTU_COUNT, cp->page_size >> (i + 10));
1336 val |= CAS_BASE(RX_PAGE_SIZE_MTU_OFF, 0x1);
1337 writel(val, cp->regs + REG_RX_PAGE_SIZE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001338
David S. Miller1f26dac2005-09-27 15:24:13 -07001339 /* enable the header parser if desired */
1340 if (CAS_HP_FIRMWARE == cas_prog_null)
1341 return;
1342
1343 val = CAS_BASE(HP_CFG_NUM_CPU, CAS_NCPUS > 63 ? 0 : CAS_NCPUS);
1344 val |= HP_CFG_PARSE_EN | HP_CFG_SYN_INC_MASK;
1345 val |= CAS_BASE(HP_CFG_TCP_THRESH, HP_TCP_THRESH_VAL);
1346 writel(val, cp->regs + REG_HP_CFG);
1347}
1348
1349static inline void cas_rxc_init(struct cas_rx_comp *rxc)
1350{
1351 memset(rxc, 0, sizeof(*rxc));
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001352 rxc->word4 = cpu_to_le64(RX_COMP4_ZERO);
David S. Miller1f26dac2005-09-27 15:24:13 -07001353}
1354
1355/* NOTE: we use the ENC RX DESC ring for spares. the rx_page[0,1]
1356 * flipping is protected by the fact that the chip will not
1357 * hand back the same page index while it's being processed.
1358 */
1359static inline cas_page_t *cas_page_spare(struct cas *cp, const int index)
1360{
1361 cas_page_t *page = cp->rx_pages[1][index];
1362 cas_page_t *new;
1363
David S. Miller9de4dfb42008-01-03 19:33:50 -08001364 if (page_count(page->buffer) == 1)
David S. Miller1f26dac2005-09-27 15:24:13 -07001365 return page;
1366
1367 new = cas_page_dequeue(cp);
1368 if (new) {
1369 spin_lock(&cp->rx_inuse_lock);
1370 list_add(&page->list, &cp->rx_inuse_list);
1371 spin_unlock(&cp->rx_inuse_lock);
1372 }
1373 return new;
1374}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001375
David S. Miller1f26dac2005-09-27 15:24:13 -07001376/* this needs to be changed if we actually use the ENC RX DESC ring */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001377static cas_page_t *cas_page_swap(struct cas *cp, const int ring,
David S. Miller1f26dac2005-09-27 15:24:13 -07001378 const int index)
1379{
1380 cas_page_t **page0 = cp->rx_pages[0];
1381 cas_page_t **page1 = cp->rx_pages[1];
1382
1383 /* swap if buffer is in use */
David S. Miller9de4dfb42008-01-03 19:33:50 -08001384 if (page_count(page0[index]->buffer) > 1) {
David S. Miller1f26dac2005-09-27 15:24:13 -07001385 cas_page_t *new = cas_page_spare(cp, index);
1386 if (new) {
1387 page1[index] = page0[index];
1388 page0[index] = new;
1389 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001390 }
David S. Miller1f26dac2005-09-27 15:24:13 -07001391 RX_USED_SET(page0[index], 0);
1392 return page0[index];
1393}
1394
1395static void cas_clean_rxds(struct cas *cp)
1396{
1397 /* only clean ring 0 as ring 1 is used for spare buffers */
1398 struct cas_rx_desc *rxd = cp->init_rxds[0];
1399 int i, size;
1400
1401 /* release all rx flows */
1402 for (i = 0; i < N_RX_FLOWS; i++) {
1403 struct sk_buff *skb;
1404 while ((skb = __skb_dequeue(&cp->rx_flows[i]))) {
1405 cas_skb_release(skb);
1406 }
1407 }
1408
1409 /* initialize descriptors */
1410 size = RX_DESC_RINGN_SIZE(0);
1411 for (i = 0; i < size; i++) {
1412 cas_page_t *page = cas_page_swap(cp, 0, i);
1413 rxd[i].buffer = cpu_to_le64(page->dma_addr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001414 rxd[i].index = cpu_to_le64(CAS_BASE(RX_INDEX_NUM, i) |
David S. Miller1f26dac2005-09-27 15:24:13 -07001415 CAS_BASE(RX_INDEX_RING, 0));
1416 }
1417
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001418 cp->rx_old[0] = RX_DESC_RINGN_SIZE(0) - 4;
David S. Miller1f26dac2005-09-27 15:24:13 -07001419 cp->rx_last[0] = 0;
1420 cp->cas_flags &= ~CAS_FLAG_RXD_POST(0);
1421}
1422
1423static void cas_clean_rxcs(struct cas *cp)
1424{
1425 int i, j;
1426
1427 /* take ownership of rx comp descriptors */
1428 memset(cp->rx_cur, 0, sizeof(*cp->rx_cur)*N_RX_COMP_RINGS);
1429 memset(cp->rx_new, 0, sizeof(*cp->rx_new)*N_RX_COMP_RINGS);
1430 for (i = 0; i < N_RX_COMP_RINGS; i++) {
1431 struct cas_rx_comp *rxc = cp->init_rxcs[i];
1432 for (j = 0; j < RX_COMP_RINGN_SIZE(i); j++) {
1433 cas_rxc_init(rxc + j);
1434 }
1435 }
1436}
1437
1438#if 0
1439/* When we get a RX fifo overflow, the RX unit is probably hung
1440 * so we do the following.
1441 *
1442 * If any part of the reset goes wrong, we return 1 and that causes the
1443 * whole chip to be reset.
1444 */
1445static int cas_rxmac_reset(struct cas *cp)
1446{
1447 struct net_device *dev = cp->dev;
1448 int limit;
1449 u32 val;
1450
1451 /* First, reset MAC RX. */
1452 writel(cp->mac_rx_cfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1453 for (limit = 0; limit < STOP_TRIES; limit++) {
1454 if (!(readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN))
1455 break;
1456 udelay(10);
1457 }
1458 if (limit == STOP_TRIES) {
Joe Perches436d27d2010-02-17 15:01:53 +00001459 netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001460 return 1;
1461 }
1462
1463 /* Second, disable RX DMA. */
1464 writel(0, cp->regs + REG_RX_CFG);
1465 for (limit = 0; limit < STOP_TRIES; limit++) {
1466 if (!(readl(cp->regs + REG_RX_CFG) & RX_CFG_DMA_EN))
1467 break;
1468 udelay(10);
1469 }
1470 if (limit == STOP_TRIES) {
Joe Perches436d27d2010-02-17 15:01:53 +00001471 netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001472 return 1;
1473 }
1474
1475 mdelay(5);
1476
1477 /* Execute RX reset command. */
1478 writel(SW_RESET_RX, cp->regs + REG_SW_RESET);
1479 for (limit = 0; limit < STOP_TRIES; limit++) {
1480 if (!(readl(cp->regs + REG_SW_RESET) & SW_RESET_RX))
1481 break;
1482 udelay(10);
1483 }
1484 if (limit == STOP_TRIES) {
Joe Perches436d27d2010-02-17 15:01:53 +00001485 netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001486 return 1;
1487 }
1488
1489 /* reset driver rx state */
1490 cas_clean_rxds(cp);
1491 cas_clean_rxcs(cp);
1492
1493 /* Now, reprogram the rest of RX unit. */
1494 cas_init_rx_dma(cp);
1495
1496 /* re-enable */
1497 val = readl(cp->regs + REG_RX_CFG);
1498 writel(val | RX_CFG_DMA_EN, cp->regs + REG_RX_CFG);
1499 writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
1500 val = readl(cp->regs + REG_MAC_RX_CFG);
1501 writel(val | MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
1502 return 0;
1503}
1504#endif
1505
1506static int cas_rxmac_interrupt(struct net_device *dev, struct cas *cp,
1507 u32 status)
1508{
1509 u32 stat = readl(cp->regs + REG_MAC_RX_STATUS);
1510
1511 if (!stat)
1512 return 0;
1513
Joe Perches436d27d2010-02-17 15:01:53 +00001514 netif_dbg(cp, intr, cp->dev, "rxmac interrupt, stat: 0x%x\n", stat);
David S. Miller1f26dac2005-09-27 15:24:13 -07001515
1516 /* these are all rollovers */
1517 spin_lock(&cp->stat_lock[0]);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001518 if (stat & MAC_RX_ALIGN_ERR)
David S. Miller1f26dac2005-09-27 15:24:13 -07001519 cp->net_stats[0].rx_frame_errors += 0x10000;
1520
1521 if (stat & MAC_RX_CRC_ERR)
1522 cp->net_stats[0].rx_crc_errors += 0x10000;
1523
1524 if (stat & MAC_RX_LEN_ERR)
1525 cp->net_stats[0].rx_length_errors += 0x10000;
1526
1527 if (stat & MAC_RX_OVERFLOW) {
1528 cp->net_stats[0].rx_over_errors++;
1529 cp->net_stats[0].rx_fifo_errors++;
1530 }
1531
1532 /* We do not track MAC_RX_FRAME_COUNT and MAC_RX_VIOL_ERR
1533 * events.
1534 */
1535 spin_unlock(&cp->stat_lock[0]);
1536 return 0;
1537}
1538
1539static int cas_mac_interrupt(struct net_device *dev, struct cas *cp,
1540 u32 status)
1541{
1542 u32 stat = readl(cp->regs + REG_MAC_CTRL_STATUS);
1543
1544 if (!stat)
1545 return 0;
1546
Joe Perches436d27d2010-02-17 15:01:53 +00001547 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1548 "mac interrupt, stat: 0x%x\n", stat);
David S. Miller1f26dac2005-09-27 15:24:13 -07001549
1550 /* This interrupt is just for pause frame and pause
1551 * tracking. It is useful for diagnostics and debug
1552 * but probably by default we will mask these events.
1553 */
1554 if (stat & MAC_CTRL_PAUSE_STATE)
1555 cp->pause_entered++;
1556
1557 if (stat & MAC_CTRL_PAUSE_RECEIVED)
1558 cp->pause_last_time_recvd = (stat >> 16);
1559
1560 return 0;
1561}
1562
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001563
David S. Miller1f26dac2005-09-27 15:24:13 -07001564/* Must be invoked under cp->lock. */
1565static inline int cas_mdio_link_not_up(struct cas *cp)
1566{
1567 u16 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001568
David S. Miller1f26dac2005-09-27 15:24:13 -07001569 switch (cp->lstate) {
1570 case link_force_ret:
Joe Perches436d27d2010-02-17 15:01:53 +00001571 netif_info(cp, link, cp->dev, "Autoneg failed again, keeping forced mode\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001572 cas_phy_write(cp, MII_BMCR, cp->link_fcntl);
1573 cp->timer_ticks = 5;
1574 cp->lstate = link_force_ok;
1575 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1576 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001577
David S. Miller1f26dac2005-09-27 15:24:13 -07001578 case link_aneg:
1579 val = cas_phy_read(cp, MII_BMCR);
1580
1581 /* Try forced modes. we try things in the following order:
1582 * 1000 full -> 100 full/half -> 10 half
1583 */
1584 val &= ~(BMCR_ANRESTART | BMCR_ANENABLE);
1585 val |= BMCR_FULLDPLX;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001586 val |= (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
David S. Miller1f26dac2005-09-27 15:24:13 -07001587 CAS_BMCR_SPEED1000 : BMCR_SPEED100;
1588 cas_phy_write(cp, MII_BMCR, val);
1589 cp->timer_ticks = 5;
1590 cp->lstate = link_force_try;
1591 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1592 break;
1593
1594 case link_force_try:
1595 /* Downgrade from 1000 to 100 to 10 Mbps if necessary. */
1596 val = cas_phy_read(cp, MII_BMCR);
1597 cp->timer_ticks = 5;
1598 if (val & CAS_BMCR_SPEED1000) { /* gigabit */
1599 val &= ~CAS_BMCR_SPEED1000;
1600 val |= (BMCR_SPEED100 | BMCR_FULLDPLX);
1601 cas_phy_write(cp, MII_BMCR, val);
1602 break;
1603 }
1604
1605 if (val & BMCR_SPEED100) {
1606 if (val & BMCR_FULLDPLX) /* fd failed */
1607 val &= ~BMCR_FULLDPLX;
1608 else { /* 100Mbps failed */
1609 val &= ~BMCR_SPEED100;
1610 }
1611 cas_phy_write(cp, MII_BMCR, val);
1612 break;
1613 }
1614 default:
1615 break;
1616 }
1617 return 0;
1618}
1619
1620
1621/* must be invoked with cp->lock held */
1622static int cas_mii_link_check(struct cas *cp, const u16 bmsr)
1623{
1624 int restart;
1625
1626 if (bmsr & BMSR_LSTATUS) {
1627 /* Ok, here we got a link. If we had it due to a forced
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001628 * fallback, and we were configured for autoneg, we
David S. Miller1f26dac2005-09-27 15:24:13 -07001629 * retry a short autoneg pass. If you know your hub is
1630 * broken, use ethtool ;)
1631 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001632 if ((cp->lstate == link_force_try) &&
David S. Miller1f26dac2005-09-27 15:24:13 -07001633 (cp->link_cntl & BMCR_ANENABLE)) {
1634 cp->lstate = link_force_ret;
1635 cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
1636 cas_mif_poll(cp, 0);
1637 cp->link_fcntl = cas_phy_read(cp, MII_BMCR);
1638 cp->timer_ticks = 5;
Joe Perches436d27d2010-02-17 15:01:53 +00001639 if (cp->opened)
1640 netif_info(cp, link, cp->dev,
1641 "Got link after fallback, retrying autoneg once...\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001642 cas_phy_write(cp, MII_BMCR,
1643 cp->link_fcntl | BMCR_ANENABLE |
1644 BMCR_ANRESTART);
1645 cas_mif_poll(cp, 1);
1646
1647 } else if (cp->lstate != link_up) {
1648 cp->lstate = link_up;
1649 cp->link_transition = LINK_TRANSITION_LINK_UP;
1650
1651 if (cp->opened) {
1652 cas_set_link_modes(cp);
1653 netif_carrier_on(cp->dev);
1654 }
1655 }
1656 return 0;
1657 }
1658
1659 /* link not up. if the link was previously up, we restart the
1660 * whole process
1661 */
1662 restart = 0;
1663 if (cp->lstate == link_up) {
1664 cp->lstate = link_down;
1665 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
1666
1667 netif_carrier_off(cp->dev);
Joe Perches436d27d2010-02-17 15:01:53 +00001668 if (cp->opened)
1669 netif_info(cp, link, cp->dev, "Link down\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001670 restart = 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001671
David S. Miller1f26dac2005-09-27 15:24:13 -07001672 } else if (++cp->timer_ticks > 10)
1673 cas_mdio_link_not_up(cp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001674
David S. Miller1f26dac2005-09-27 15:24:13 -07001675 return restart;
1676}
1677
1678static int cas_mif_interrupt(struct net_device *dev, struct cas *cp,
1679 u32 status)
1680{
1681 u32 stat = readl(cp->regs + REG_MIF_STATUS);
1682 u16 bmsr;
1683
1684 /* check for a link change */
1685 if (CAS_VAL(MIF_STATUS_POLL_STATUS, stat) == 0)
1686 return 0;
1687
1688 bmsr = CAS_VAL(MIF_STATUS_POLL_DATA, stat);
1689 return cas_mii_link_check(cp, bmsr);
1690}
1691
1692static int cas_pci_interrupt(struct net_device *dev, struct cas *cp,
1693 u32 status)
1694{
1695 u32 stat = readl(cp->regs + REG_PCI_ERR_STATUS);
1696
1697 if (!stat)
1698 return 0;
1699
Joe Perches436d27d2010-02-17 15:01:53 +00001700 netdev_err(dev, "PCI error [%04x:%04x]",
1701 stat, readl(cp->regs + REG_BIM_DIAG));
David S. Miller1f26dac2005-09-27 15:24:13 -07001702
1703 /* cassini+ has this reserved */
1704 if ((stat & PCI_ERR_BADACK) &&
1705 ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0))
Joe Perches436d27d2010-02-17 15:01:53 +00001706 pr_cont(" <No ACK64# during ABS64 cycle>");
David S. Miller1f26dac2005-09-27 15:24:13 -07001707
1708 if (stat & PCI_ERR_DTRTO)
Joe Perches436d27d2010-02-17 15:01:53 +00001709 pr_cont(" <Delayed transaction timeout>");
David S. Miller1f26dac2005-09-27 15:24:13 -07001710 if (stat & PCI_ERR_OTHER)
Joe Perches436d27d2010-02-17 15:01:53 +00001711 pr_cont(" <other>");
David S. Miller1f26dac2005-09-27 15:24:13 -07001712 if (stat & PCI_ERR_BIM_DMA_WRITE)
Joe Perches436d27d2010-02-17 15:01:53 +00001713 pr_cont(" <BIM DMA 0 write req>");
David S. Miller1f26dac2005-09-27 15:24:13 -07001714 if (stat & PCI_ERR_BIM_DMA_READ)
Joe Perches436d27d2010-02-17 15:01:53 +00001715 pr_cont(" <BIM DMA 0 read req>");
1716 pr_cont("\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001717
1718 if (stat & PCI_ERR_OTHER) {
1719 u16 cfg;
1720
1721 /* Interrogate PCI config space for the
1722 * true cause.
1723 */
1724 pci_read_config_word(cp->pdev, PCI_STATUS, &cfg);
Joe Perches436d27d2010-02-17 15:01:53 +00001725 netdev_err(dev, "Read PCI cfg space status [%04x]\n", cfg);
David S. Miller1f26dac2005-09-27 15:24:13 -07001726 if (cfg & PCI_STATUS_PARITY)
Joe Perches436d27d2010-02-17 15:01:53 +00001727 netdev_err(dev, "PCI parity error detected\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001728 if (cfg & PCI_STATUS_SIG_TARGET_ABORT)
Joe Perches436d27d2010-02-17 15:01:53 +00001729 netdev_err(dev, "PCI target abort\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001730 if (cfg & PCI_STATUS_REC_TARGET_ABORT)
Joe Perches436d27d2010-02-17 15:01:53 +00001731 netdev_err(dev, "PCI master acks target abort\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001732 if (cfg & PCI_STATUS_REC_MASTER_ABORT)
Joe Perches436d27d2010-02-17 15:01:53 +00001733 netdev_err(dev, "PCI master abort\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001734 if (cfg & PCI_STATUS_SIG_SYSTEM_ERROR)
Joe Perches436d27d2010-02-17 15:01:53 +00001735 netdev_err(dev, "PCI system error SERR#\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001736 if (cfg & PCI_STATUS_DETECTED_PARITY)
Joe Perches436d27d2010-02-17 15:01:53 +00001737 netdev_err(dev, "PCI parity error\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001738
1739 /* Write the error bits back to clear them. */
1740 cfg &= (PCI_STATUS_PARITY |
1741 PCI_STATUS_SIG_TARGET_ABORT |
1742 PCI_STATUS_REC_TARGET_ABORT |
1743 PCI_STATUS_REC_MASTER_ABORT |
1744 PCI_STATUS_SIG_SYSTEM_ERROR |
1745 PCI_STATUS_DETECTED_PARITY);
1746 pci_write_config_word(cp->pdev, PCI_STATUS, cfg);
1747 }
1748
1749 /* For all PCI errors, we should reset the chip. */
1750 return 1;
1751}
1752
1753/* All non-normal interrupt conditions get serviced here.
1754 * Returns non-zero if we should just exit the interrupt
1755 * handler right now (ie. if we reset the card which invalidates
1756 * all of the other original irq status bits).
1757 */
1758static int cas_abnormal_irq(struct net_device *dev, struct cas *cp,
1759 u32 status)
1760{
1761 if (status & INTR_RX_TAG_ERROR) {
1762 /* corrupt RX tag framing */
Joe Perches436d27d2010-02-17 15:01:53 +00001763 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1764 "corrupt rx tag framing\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001765 spin_lock(&cp->stat_lock[0]);
1766 cp->net_stats[0].rx_errors++;
1767 spin_unlock(&cp->stat_lock[0]);
1768 goto do_reset;
1769 }
1770
1771 if (status & INTR_RX_LEN_MISMATCH) {
1772 /* length mismatch. */
Joe Perches436d27d2010-02-17 15:01:53 +00001773 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
1774 "length mismatch for rx frame\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001775 spin_lock(&cp->stat_lock[0]);
1776 cp->net_stats[0].rx_errors++;
1777 spin_unlock(&cp->stat_lock[0]);
1778 goto do_reset;
1779 }
1780
1781 if (status & INTR_PCS_STATUS) {
1782 if (cas_pcs_interrupt(dev, cp, status))
1783 goto do_reset;
1784 }
1785
1786 if (status & INTR_TX_MAC_STATUS) {
1787 if (cas_txmac_interrupt(dev, cp, status))
1788 goto do_reset;
1789 }
1790
1791 if (status & INTR_RX_MAC_STATUS) {
1792 if (cas_rxmac_interrupt(dev, cp, status))
1793 goto do_reset;
1794 }
1795
1796 if (status & INTR_MAC_CTRL_STATUS) {
1797 if (cas_mac_interrupt(dev, cp, status))
1798 goto do_reset;
1799 }
1800
1801 if (status & INTR_MIF_STATUS) {
1802 if (cas_mif_interrupt(dev, cp, status))
1803 goto do_reset;
1804 }
1805
1806 if (status & INTR_PCI_ERROR_STATUS) {
1807 if (cas_pci_interrupt(dev, cp, status))
1808 goto do_reset;
1809 }
1810 return 0;
1811
1812do_reset:
1813#if 1
1814 atomic_inc(&cp->reset_task_pending);
1815 atomic_inc(&cp->reset_task_pending_all);
Joe Perches436d27d2010-02-17 15:01:53 +00001816 netdev_err(dev, "reset called in cas_abnormal_irq [0x%x]\n", status);
David S. Miller1f26dac2005-09-27 15:24:13 -07001817 schedule_work(&cp->reset_task);
1818#else
1819 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
Joe Perches436d27d2010-02-17 15:01:53 +00001820 netdev_err(dev, "reset called in cas_abnormal_irq\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07001821 schedule_work(&cp->reset_task);
1822#endif
1823 return 1;
1824}
1825
1826/* NOTE: CAS_TABORT returns 1 or 2 so that it can be used when
1827 * determining whether to do a netif_stop/wakeup
1828 */
1829#define CAS_TABORT(x) (((x)->cas_flags & CAS_FLAG_TARGET_ABORT) ? 2 : 1)
1830#define CAS_ROUND_PAGE(x) (((x) + PAGE_SIZE - 1) & PAGE_MASK)
1831static inline int cas_calc_tabort(struct cas *cp, const unsigned long addr,
1832 const int len)
1833{
1834 unsigned long off = addr + len;
1835
1836 if (CAS_TABORT(cp) == 1)
1837 return 0;
1838 if ((CAS_ROUND_PAGE(off) - off) > TX_TARGET_ABORT_LEN)
1839 return 0;
1840 return TX_TARGET_ABORT_LEN;
1841}
1842
1843static inline void cas_tx_ringN(struct cas *cp, int ring, int limit)
1844{
1845 struct cas_tx_desc *txds;
1846 struct sk_buff **skbs;
1847 struct net_device *dev = cp->dev;
1848 int entry, count;
1849
1850 spin_lock(&cp->tx_lock[ring]);
1851 txds = cp->init_txds[ring];
1852 skbs = cp->tx_skbs[ring];
1853 entry = cp->tx_old[ring];
1854
1855 count = TX_BUFF_COUNT(ring, entry, limit);
1856 while (entry != limit) {
1857 struct sk_buff *skb = skbs[entry];
1858 dma_addr_t daddr;
1859 u32 dlen;
1860 int frag;
1861
1862 if (!skb) {
1863 /* this should never occur */
1864 entry = TX_DESC_NEXT(ring, entry);
1865 continue;
1866 }
1867
1868 /* however, we might get only a partial skb release. */
1869 count -= skb_shinfo(skb)->nr_frags +
1870 + cp->tx_tiny_use[ring][entry].nbufs + 1;
1871 if (count < 0)
1872 break;
1873
Joe Perches436d27d2010-02-17 15:01:53 +00001874 netif_printk(cp, tx_done, KERN_DEBUG, cp->dev,
1875 "tx[%d] done, slot %d\n", ring, entry);
David S. Miller1f26dac2005-09-27 15:24:13 -07001876
1877 skbs[entry] = NULL;
1878 cp->tx_tiny_use[ring][entry].nbufs = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001879
David S. Miller1f26dac2005-09-27 15:24:13 -07001880 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1881 struct cas_tx_desc *txd = txds + entry;
1882
1883 daddr = le64_to_cpu(txd->buffer);
1884 dlen = CAS_VAL(TX_DESC_BUFLEN,
1885 le64_to_cpu(txd->control));
1886 pci_unmap_page(cp->pdev, daddr, dlen,
1887 PCI_DMA_TODEVICE);
1888 entry = TX_DESC_NEXT(ring, entry);
1889
1890 /* tiny buffer may follow */
1891 if (cp->tx_tiny_use[ring][entry].used) {
1892 cp->tx_tiny_use[ring][entry].used = 0;
1893 entry = TX_DESC_NEXT(ring, entry);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001894 }
David S. Miller1f26dac2005-09-27 15:24:13 -07001895 }
1896
1897 spin_lock(&cp->stat_lock[ring]);
1898 cp->net_stats[ring].tx_packets++;
1899 cp->net_stats[ring].tx_bytes += skb->len;
1900 spin_unlock(&cp->stat_lock[ring]);
Yang Wei98fcd702019-02-06 00:19:44 +08001901 dev_consume_skb_irq(skb);
David S. Miller1f26dac2005-09-27 15:24:13 -07001902 }
1903 cp->tx_old[ring] = entry;
1904
1905 /* this is wrong for multiple tx rings. the net device needs
1906 * multiple queues for this to do the right thing. we wait
1907 * for 2*packets to be available when using tiny buffers
1908 */
1909 if (netif_queue_stopped(dev) &&
1910 (TX_BUFFS_AVAIL(cp, ring) > CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1)))
1911 netif_wake_queue(dev);
1912 spin_unlock(&cp->tx_lock[ring]);
1913}
1914
1915static void cas_tx(struct net_device *dev, struct cas *cp,
1916 u32 status)
1917{
1918 int limit, ring;
1919#ifdef USE_TX_COMPWB
1920 u64 compwb = le64_to_cpu(cp->init_block->tx_compwb);
1921#endif
Joe Perches436d27d2010-02-17 15:01:53 +00001922 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
1923 "tx interrupt, status: 0x%x, %llx\n",
1924 status, (unsigned long long)compwb);
David S. Miller1f26dac2005-09-27 15:24:13 -07001925 /* process all the rings */
1926 for (ring = 0; ring < N_TX_RINGS; ring++) {
1927#ifdef USE_TX_COMPWB
1928 /* use the completion writeback registers */
1929 limit = (CAS_VAL(TX_COMPWB_MSB, compwb) << 8) |
1930 CAS_VAL(TX_COMPWB_LSB, compwb);
1931 compwb = TX_COMPWB_NEXT(compwb);
1932#else
1933 limit = readl(cp->regs + REG_TX_COMPN(ring));
1934#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001935 if (cp->tx_old[ring] != limit)
David S. Miller1f26dac2005-09-27 15:24:13 -07001936 cas_tx_ringN(cp, ring, limit);
1937 }
1938}
1939
1940
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001941static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
1942 int entry, const u64 *words,
David S. Miller1f26dac2005-09-27 15:24:13 -07001943 struct sk_buff **skbref)
1944{
1945 int dlen, hlen, len, i, alloclen;
1946 int off, swivel = RX_SWIVEL_OFF_VAL;
1947 struct cas_page *page;
1948 struct sk_buff *skb;
1949 void *addr, *crcaddr;
Al Viroe5e02542008-01-03 18:49:00 -08001950 __sum16 csum;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001951 char *p;
David S. Miller1f26dac2005-09-27 15:24:13 -07001952
1953 hlen = CAS_VAL(RX_COMP2_HDR_SIZE, words[1]);
1954 dlen = CAS_VAL(RX_COMP1_DATA_SIZE, words[0]);
1955 len = hlen + dlen;
1956
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001957 if (RX_COPY_ALWAYS || (words[2] & RX_COMP3_SMALL_PKT))
David S. Miller1f26dac2005-09-27 15:24:13 -07001958 alloclen = len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001959 else
David S. Miller1f26dac2005-09-27 15:24:13 -07001960 alloclen = max(hlen, RX_COPY_MIN);
1961
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001962 skb = netdev_alloc_skb(cp->dev, alloclen + swivel + cp->crc_size);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001963 if (skb == NULL)
David S. Miller1f26dac2005-09-27 15:24:13 -07001964 return -1;
1965
1966 *skbref = skb;
David S. Miller1f26dac2005-09-27 15:24:13 -07001967 skb_reserve(skb, swivel);
1968
1969 p = skb->data;
1970 addr = crcaddr = NULL;
1971 if (hlen) { /* always copy header pages */
1972 i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
1973 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001974 off = CAS_VAL(RX_COMP2_HDR_OFF, words[1]) * 0x100 +
David S. Miller1f26dac2005-09-27 15:24:13 -07001975 swivel;
1976
1977 i = hlen;
1978 if (!dlen) /* attach FCS */
1979 i += cp->crc_size;
1980 pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
1981 PCI_DMA_FROMDEVICE);
1982 addr = cas_page_map(page->buffer);
1983 memcpy(p, addr + off, i);
1984 pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
1985 PCI_DMA_FROMDEVICE);
1986 cas_page_unmap(addr);
1987 RX_USED_ADD(page, 0x100);
1988 p += hlen;
1989 swivel = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001990 }
David S. Miller1f26dac2005-09-27 15:24:13 -07001991
1992
1993 if (alloclen < (hlen + dlen)) {
1994 skb_frag_t *frag = skb_shinfo(skb)->frags;
1995
1996 /* normal or jumbo packets. we use frags */
1997 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
1998 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
1999 off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
2000
2001 hlen = min(cp->page_size - off, dlen);
2002 if (hlen < 0) {
Joe Perches436d27d2010-02-17 15:01:53 +00002003 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
2004 "rx page overflow: %d\n", hlen);
David S. Miller1f26dac2005-09-27 15:24:13 -07002005 dev_kfree_skb_irq(skb);
2006 return -1;
2007 }
2008 i = hlen;
2009 if (i == dlen) /* attach FCS */
2010 i += cp->crc_size;
2011 pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
2012 PCI_DMA_FROMDEVICE);
2013
2014 /* make sure we always copy a header */
2015 swivel = 0;
2016 if (p == (char *) skb->data) { /* not split */
2017 addr = cas_page_map(page->buffer);
2018 memcpy(p, addr + off, RX_COPY_MIN);
2019 pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
2020 PCI_DMA_FROMDEVICE);
2021 cas_page_unmap(addr);
2022 off += RX_COPY_MIN;
2023 swivel = RX_COPY_MIN;
2024 RX_USED_ADD(page, cp->mtu_stride);
2025 } else {
2026 RX_USED_ADD(page, hlen);
2027 }
2028 skb_put(skb, alloclen);
2029
2030 skb_shinfo(skb)->nr_frags++;
2031 skb->data_len += hlen - swivel;
David S. Millerd011a232008-01-04 00:03:56 -08002032 skb->truesize += hlen - swivel;
David S. Miller1f26dac2005-09-27 15:24:13 -07002033 skb->len += hlen - swivel;
2034
Ian Campbell18324d692011-08-29 23:18:25 +00002035 __skb_frag_set_page(frag, page->buffer);
2036 __skb_frag_ref(frag);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002037 skb_frag_off_set(frag, off);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002038 skb_frag_size_set(frag, hlen - swivel);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002039
David S. Miller1f26dac2005-09-27 15:24:13 -07002040 /* any more data? */
2041 if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2042 hlen = dlen;
2043 off = 0;
2044
2045 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2046 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002047 pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
2048 hlen + cp->crc_size,
David S. Miller1f26dac2005-09-27 15:24:13 -07002049 PCI_DMA_FROMDEVICE);
2050 pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
2051 hlen + cp->crc_size,
2052 PCI_DMA_FROMDEVICE);
2053
2054 skb_shinfo(skb)->nr_frags++;
2055 skb->data_len += hlen;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002056 skb->len += hlen;
David S. Miller1f26dac2005-09-27 15:24:13 -07002057 frag++;
2058
Ian Campbell18324d692011-08-29 23:18:25 +00002059 __skb_frag_set_page(frag, page->buffer);
2060 __skb_frag_ref(frag);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002061 skb_frag_off_set(frag, 0);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002062 skb_frag_size_set(frag, hlen);
David S. Miller1f26dac2005-09-27 15:24:13 -07002063 RX_USED_ADD(page, hlen + cp->crc_size);
2064 }
2065
2066 if (cp->crc_size) {
2067 addr = cas_page_map(page->buffer);
2068 crcaddr = addr + off + hlen;
2069 }
2070
2071 } else {
2072 /* copying packet */
2073 if (!dlen)
2074 goto end_copy_pkt;
2075
2076 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2077 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
2078 off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
2079 hlen = min(cp->page_size - off, dlen);
2080 if (hlen < 0) {
Joe Perches436d27d2010-02-17 15:01:53 +00002081 netif_printk(cp, rx_err, KERN_DEBUG, cp->dev,
2082 "rx page overflow: %d\n", hlen);
David S. Miller1f26dac2005-09-27 15:24:13 -07002083 dev_kfree_skb_irq(skb);
2084 return -1;
2085 }
2086 i = hlen;
2087 if (i == dlen) /* attach FCS */
2088 i += cp->crc_size;
2089 pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
2090 PCI_DMA_FROMDEVICE);
2091 addr = cas_page_map(page->buffer);
2092 memcpy(p, addr + off, i);
2093 pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
2094 PCI_DMA_FROMDEVICE);
2095 cas_page_unmap(addr);
2096 if (p == (char *) skb->data) /* not split */
2097 RX_USED_ADD(page, cp->mtu_stride);
2098 else
2099 RX_USED_ADD(page, i);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002100
David S. Miller1f26dac2005-09-27 15:24:13 -07002101 /* any more data? */
2102 if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
2103 p += hlen;
2104 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2105 page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002106 pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
2107 dlen + cp->crc_size,
David S. Miller1f26dac2005-09-27 15:24:13 -07002108 PCI_DMA_FROMDEVICE);
2109 addr = cas_page_map(page->buffer);
2110 memcpy(p, addr, dlen + cp->crc_size);
2111 pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
2112 dlen + cp->crc_size,
2113 PCI_DMA_FROMDEVICE);
2114 cas_page_unmap(addr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002115 RX_USED_ADD(page, dlen + cp->crc_size);
David S. Miller1f26dac2005-09-27 15:24:13 -07002116 }
2117end_copy_pkt:
2118 if (cp->crc_size) {
2119 addr = NULL;
2120 crcaddr = skb->data + alloclen;
2121 }
2122 skb_put(skb, alloclen);
2123 }
2124
Al Viroe5e02542008-01-03 18:49:00 -08002125 csum = (__force __sum16)htons(CAS_VAL(RX_COMP4_TCP_CSUM, words[3]));
David S. Miller1f26dac2005-09-27 15:24:13 -07002126 if (cp->crc_size) {
2127 /* checksum includes FCS. strip it out. */
Al Viroe5e02542008-01-03 18:49:00 -08002128 csum = csum_fold(csum_partial(crcaddr, cp->crc_size,
2129 csum_unfold(csum)));
David S. Miller1f26dac2005-09-27 15:24:13 -07002130 if (addr)
2131 cas_page_unmap(addr);
2132 }
David S. Miller1f26dac2005-09-27 15:24:13 -07002133 skb->protocol = eth_type_trans(skb, cp->dev);
David S. Millerb1443e22008-05-21 17:05:34 -07002134 if (skb->protocol == htons(ETH_P_IP)) {
2135 skb->csum = csum_unfold(~csum);
2136 skb->ip_summed = CHECKSUM_COMPLETE;
2137 } else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002138 skb_checksum_none_assert(skb);
David S. Miller1f26dac2005-09-27 15:24:13 -07002139 return len;
2140}
2141
2142
2143/* we can handle up to 64 rx flows at a time. we do the same thing
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002144 * as nonreassm except that we batch up the buffers.
David S. Miller1f26dac2005-09-27 15:24:13 -07002145 * NOTE: we currently just treat each flow as a bunch of packets that
2146 * we pass up. a better way would be to coalesce the packets
2147 * into a jumbo packet. to do that, we need to do the following:
2148 * 1) the first packet will have a clean split between header and
2149 * data. save both.
2150 * 2) each time the next flow packet comes in, extend the
2151 * data length and merge the checksums.
2152 * 3) on flow release, fix up the header.
2153 * 4) make sure the higher layer doesn't care.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002154 * because packets get coalesced, we shouldn't run into fragment count
David S. Miller1f26dac2005-09-27 15:24:13 -07002155 * issues.
2156 */
2157static inline void cas_rx_flow_pkt(struct cas *cp, const u64 *words,
2158 struct sk_buff *skb)
2159{
2160 int flowid = CAS_VAL(RX_COMP3_FLOWID, words[2]) & (N_RX_FLOWS - 1);
2161 struct sk_buff_head *flow = &cp->rx_flows[flowid];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002162
2163 /* this is protected at a higher layer, so no need to
David S. Miller1f26dac2005-09-27 15:24:13 -07002164 * do any additional locking here. stick the buffer
2165 * at the end.
2166 */
David S. Miller43f59c82008-09-21 21:28:51 -07002167 __skb_queue_tail(flow, skb);
David S. Miller1f26dac2005-09-27 15:24:13 -07002168 if (words[0] & RX_COMP1_RELEASE_FLOW) {
2169 while ((skb = __skb_dequeue(flow))) {
2170 cas_skb_release(skb);
2171 }
2172 }
2173}
2174
2175/* put rx descriptor back on ring. if a buffer is in use by a higher
2176 * layer, this will need to put in a replacement.
2177 */
2178static void cas_post_page(struct cas *cp, const int ring, const int index)
2179{
2180 cas_page_t *new;
2181 int entry;
2182
2183 entry = cp->rx_old[ring];
2184
2185 new = cas_page_swap(cp, ring, index);
2186 cp->init_rxds[ring][entry].buffer = cpu_to_le64(new->dma_addr);
2187 cp->init_rxds[ring][entry].index =
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002188 cpu_to_le64(CAS_BASE(RX_INDEX_NUM, index) |
David S. Miller1f26dac2005-09-27 15:24:13 -07002189 CAS_BASE(RX_INDEX_RING, ring));
2190
2191 entry = RX_DESC_ENTRY(ring, entry + 1);
2192 cp->rx_old[ring] = entry;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002193
David S. Miller1f26dac2005-09-27 15:24:13 -07002194 if (entry % 4)
2195 return;
2196
2197 if (ring == 0)
2198 writel(entry, cp->regs + REG_RX_KICK);
2199 else if ((N_RX_DESC_RINGS > 1) &&
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002200 (cp->cas_flags & CAS_FLAG_REG_PLUS))
David S. Miller1f26dac2005-09-27 15:24:13 -07002201 writel(entry, cp->regs + REG_PLUS_RX_KICK1);
2202}
2203
2204
2205/* only when things are bad */
2206static int cas_post_rxds_ringN(struct cas *cp, int ring, int num)
2207{
2208 unsigned int entry, last, count, released;
2209 int cluster;
2210 cas_page_t **page = cp->rx_pages[ring];
2211
2212 entry = cp->rx_old[ring];
2213
Joe Perches436d27d2010-02-17 15:01:53 +00002214 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2215 "rxd[%d] interrupt, done: %d\n", ring, entry);
David S. Miller1f26dac2005-09-27 15:24:13 -07002216
2217 cluster = -1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002218 count = entry & 0x3;
David S. Miller1f26dac2005-09-27 15:24:13 -07002219 last = RX_DESC_ENTRY(ring, num ? entry + num - 4: entry - 4);
2220 released = 0;
2221 while (entry != last) {
2222 /* make a new buffer if it's still in use */
David S. Miller9de4dfb42008-01-03 19:33:50 -08002223 if (page_count(page[entry]->buffer) > 1) {
David S. Miller1f26dac2005-09-27 15:24:13 -07002224 cas_page_t *new = cas_page_dequeue(cp);
2225 if (!new) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002226 /* let the timer know that we need to
David S. Miller1f26dac2005-09-27 15:24:13 -07002227 * do this again
2228 */
2229 cp->cas_flags |= CAS_FLAG_RXD_POST(ring);
2230 if (!timer_pending(&cp->link_timer))
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002231 mod_timer(&cp->link_timer, jiffies +
David S. Miller1f26dac2005-09-27 15:24:13 -07002232 CAS_LINK_FAST_TIMEOUT);
2233 cp->rx_old[ring] = entry;
2234 cp->rx_last[ring] = num ? num - released : 0;
2235 return -ENOMEM;
2236 }
2237 spin_lock(&cp->rx_inuse_lock);
2238 list_add(&page[entry]->list, &cp->rx_inuse_list);
2239 spin_unlock(&cp->rx_inuse_lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002240 cp->init_rxds[ring][entry].buffer =
David S. Miller1f26dac2005-09-27 15:24:13 -07002241 cpu_to_le64(new->dma_addr);
2242 page[entry] = new;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002243
David S. Miller1f26dac2005-09-27 15:24:13 -07002244 }
2245
2246 if (++count == 4) {
2247 cluster = entry;
2248 count = 0;
2249 }
2250 released++;
2251 entry = RX_DESC_ENTRY(ring, entry + 1);
2252 }
2253 cp->rx_old[ring] = entry;
2254
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002255 if (cluster < 0)
David S. Miller1f26dac2005-09-27 15:24:13 -07002256 return 0;
2257
2258 if (ring == 0)
2259 writel(cluster, cp->regs + REG_RX_KICK);
2260 else if ((N_RX_DESC_RINGS > 1) &&
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002261 (cp->cas_flags & CAS_FLAG_REG_PLUS))
David S. Miller1f26dac2005-09-27 15:24:13 -07002262 writel(cluster, cp->regs + REG_PLUS_RX_KICK1);
2263 return 0;
2264}
2265
2266
2267/* process a completion ring. packets are set up in three basic ways:
2268 * small packets: should be copied header + data in single buffer.
2269 * large packets: header and data in a single buffer.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002270 * split packets: header in a separate buffer from data.
David S. Miller1f26dac2005-09-27 15:24:13 -07002271 * data may be in multiple pages. data may be > 256
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002272 * bytes but in a single page.
David S. Miller1f26dac2005-09-27 15:24:13 -07002273 *
2274 * NOTE: RX page posting is done in this routine as well. while there's
2275 * the capability of using multiple RX completion rings, it isn't
2276 * really worthwhile due to the fact that the page posting will
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002277 * force serialization on the single descriptor ring.
David S. Miller1f26dac2005-09-27 15:24:13 -07002278 */
2279static int cas_rx_ringN(struct cas *cp, int ring, int budget)
2280{
2281 struct cas_rx_comp *rxcs = cp->init_rxcs[ring];
2282 int entry, drops;
2283 int npackets = 0;
2284
Joe Perches436d27d2010-02-17 15:01:53 +00002285 netif_printk(cp, intr, KERN_DEBUG, cp->dev,
2286 "rx[%d] interrupt, done: %d/%d\n",
2287 ring,
2288 readl(cp->regs + REG_RX_COMP_HEAD), cp->rx_new[ring]);
David S. Miller1f26dac2005-09-27 15:24:13 -07002289
2290 entry = cp->rx_new[ring];
2291 drops = 0;
2292 while (1) {
2293 struct cas_rx_comp *rxc = rxcs + entry;
Ingo Molnarb71e8392008-11-25 16:57:05 -08002294 struct sk_buff *uninitialized_var(skb);
David S. Miller1f26dac2005-09-27 15:24:13 -07002295 int type, len;
2296 u64 words[4];
2297 int i, dring;
2298
2299 words[0] = le64_to_cpu(rxc->word1);
2300 words[1] = le64_to_cpu(rxc->word2);
2301 words[2] = le64_to_cpu(rxc->word3);
2302 words[3] = le64_to_cpu(rxc->word4);
2303
2304 /* don't touch if still owned by hw */
2305 type = CAS_VAL(RX_COMP1_TYPE, words[0]);
2306 if (type == 0)
2307 break;
2308
2309 /* hw hasn't cleared the zero bit yet */
2310 if (words[3] & RX_COMP4_ZERO) {
2311 break;
2312 }
2313
2314 /* get info on the packet */
2315 if (words[3] & (RX_COMP4_LEN_MISMATCH | RX_COMP4_BAD)) {
2316 spin_lock(&cp->stat_lock[ring]);
2317 cp->net_stats[ring].rx_errors++;
2318 if (words[3] & RX_COMP4_LEN_MISMATCH)
2319 cp->net_stats[ring].rx_length_errors++;
2320 if (words[3] & RX_COMP4_BAD)
2321 cp->net_stats[ring].rx_crc_errors++;
2322 spin_unlock(&cp->stat_lock[ring]);
2323
2324 /* We'll just return it to Cassini. */
2325 drop_it:
2326 spin_lock(&cp->stat_lock[ring]);
2327 ++cp->net_stats[ring].rx_dropped;
2328 spin_unlock(&cp->stat_lock[ring]);
2329 goto next;
2330 }
2331
2332 len = cas_rx_process_pkt(cp, rxc, entry, words, &skb);
2333 if (len < 0) {
2334 ++drops;
2335 goto drop_it;
2336 }
2337
2338 /* see if it's a flow re-assembly or not. the driver
2339 * itself handles release back up.
2340 */
2341 if (RX_DONT_BATCH || (type == 0x2)) {
2342 /* non-reassm: these always get released */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002343 cas_skb_release(skb);
David S. Miller1f26dac2005-09-27 15:24:13 -07002344 } else {
2345 cas_rx_flow_pkt(cp, words, skb);
2346 }
2347
2348 spin_lock(&cp->stat_lock[ring]);
2349 cp->net_stats[ring].rx_packets++;
2350 cp->net_stats[ring].rx_bytes += len;
2351 spin_unlock(&cp->stat_lock[ring]);
David S. Miller1f26dac2005-09-27 15:24:13 -07002352
2353 next:
2354 npackets++;
2355
2356 /* should it be released? */
2357 if (words[0] & RX_COMP1_RELEASE_HDR) {
2358 i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
2359 dring = CAS_VAL(RX_INDEX_RING, i);
2360 i = CAS_VAL(RX_INDEX_NUM, i);
2361 cas_post_page(cp, dring, i);
2362 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002363
David S. Miller1f26dac2005-09-27 15:24:13 -07002364 if (words[0] & RX_COMP1_RELEASE_DATA) {
2365 i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
2366 dring = CAS_VAL(RX_INDEX_RING, i);
2367 i = CAS_VAL(RX_INDEX_NUM, i);
2368 cas_post_page(cp, dring, i);
2369 }
2370
2371 if (words[0] & RX_COMP1_RELEASE_NEXT) {
2372 i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
2373 dring = CAS_VAL(RX_INDEX_RING, i);
2374 i = CAS_VAL(RX_INDEX_NUM, i);
2375 cas_post_page(cp, dring, i);
2376 }
2377
2378 /* skip to the next entry */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002379 entry = RX_COMP_ENTRY(ring, entry + 1 +
David S. Miller1f26dac2005-09-27 15:24:13 -07002380 CAS_VAL(RX_COMP1_SKIP, words[0]));
2381#ifdef USE_NAPI
2382 if (budget && (npackets >= budget))
2383 break;
2384#endif
2385 }
2386 cp->rx_new[ring] = entry;
2387
2388 if (drops)
Joe Perches436d27d2010-02-17 15:01:53 +00002389 netdev_info(cp->dev, "Memory squeeze, deferring packet\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07002390 return npackets;
2391}
2392
2393
2394/* put completion entries back on the ring */
2395static void cas_post_rxcs_ringN(struct net_device *dev,
2396 struct cas *cp, int ring)
2397{
2398 struct cas_rx_comp *rxc = cp->init_rxcs[ring];
2399 int last, entry;
2400
2401 last = cp->rx_cur[ring];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002402 entry = cp->rx_new[ring];
Joe Perches436d27d2010-02-17 15:01:53 +00002403 netif_printk(cp, intr, KERN_DEBUG, dev,
2404 "rxc[%d] interrupt, done: %d/%d\n",
2405 ring, readl(cp->regs + REG_RX_COMP_HEAD), entry);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002406
David S. Miller1f26dac2005-09-27 15:24:13 -07002407 /* zero and re-mark descriptors */
2408 while (last != entry) {
2409 cas_rxc_init(rxc + last);
2410 last = RX_COMP_ENTRY(ring, last + 1);
2411 }
2412 cp->rx_cur[ring] = last;
2413
2414 if (ring == 0)
2415 writel(last, cp->regs + REG_RX_COMP_TAIL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002416 else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
David S. Miller1f26dac2005-09-27 15:24:13 -07002417 writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
2418}
2419
2420
2421
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002422/* cassini can use all four PCI interrupts for the completion ring.
David S. Miller1f26dac2005-09-27 15:24:13 -07002423 * rings 3 and 4 are identical
2424 */
2425#if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002426static inline void cas_handle_irqN(struct net_device *dev,
David S. Miller1f26dac2005-09-27 15:24:13 -07002427 struct cas *cp, const u32 status,
2428 const int ring)
2429{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002430 if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
David S. Miller1f26dac2005-09-27 15:24:13 -07002431 cas_post_rxcs_ringN(dev, cp, ring);
2432}
2433
David Howells7d12e782006-10-05 14:55:46 +01002434static irqreturn_t cas_interruptN(int irq, void *dev_id)
David S. Miller1f26dac2005-09-27 15:24:13 -07002435{
2436 struct net_device *dev = dev_id;
2437 struct cas *cp = netdev_priv(dev);
2438 unsigned long flags;
David S. Miller8decf862011-09-22 03:23:13 -04002439 int ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
David S. Miller1f26dac2005-09-27 15:24:13 -07002440 u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
2441
2442 /* check for shared irq */
2443 if (status == 0)
2444 return IRQ_NONE;
2445
David S. Miller1f26dac2005-09-27 15:24:13 -07002446 spin_lock_irqsave(&cp->lock, flags);
2447 if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2448#ifdef USE_NAPI
2449 cas_mask_intr(cp);
Ben Hutchings288379f2009-01-19 16:43:59 -08002450 napi_schedule(&cp->napi);
David S. Miller1f26dac2005-09-27 15:24:13 -07002451#else
2452 cas_rx_ringN(cp, ring, 0);
2453#endif
2454 status &= ~INTR_RX_DONE_ALT;
2455 }
2456
2457 if (status)
2458 cas_handle_irqN(dev, cp, status, ring);
2459 spin_unlock_irqrestore(&cp->lock, flags);
2460 return IRQ_HANDLED;
2461}
2462#endif
2463
2464#ifdef USE_PCI_INTB
2465/* everything but rx packets */
2466static inline void cas_handle_irq1(struct cas *cp, const u32 status)
2467{
2468 if (status & INTR_RX_BUF_UNAVAIL_1) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002469 /* Frame arrived, no free RX buffers available.
David S. Miller1f26dac2005-09-27 15:24:13 -07002470 * NOTE: we can get this on a link transition. */
2471 cas_post_rxds_ringN(cp, 1, 0);
2472 spin_lock(&cp->stat_lock[1]);
2473 cp->net_stats[1].rx_dropped++;
2474 spin_unlock(&cp->stat_lock[1]);
2475 }
2476
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002477 if (status & INTR_RX_BUF_AE_1)
2478 cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
David S. Miller1f26dac2005-09-27 15:24:13 -07002479 RX_AE_FREEN_VAL(1));
2480
2481 if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2482 cas_post_rxcs_ringN(cp, 1);
2483}
2484
2485/* ring 2 handles a few more events than 3 and 4 */
David Howells7d12e782006-10-05 14:55:46 +01002486static irqreturn_t cas_interrupt1(int irq, void *dev_id)
David S. Miller1f26dac2005-09-27 15:24:13 -07002487{
2488 struct net_device *dev = dev_id;
2489 struct cas *cp = netdev_priv(dev);
2490 unsigned long flags;
2491 u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2492
2493 /* check for shared interrupt */
2494 if (status == 0)
2495 return IRQ_NONE;
2496
2497 spin_lock_irqsave(&cp->lock, flags);
2498 if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
2499#ifdef USE_NAPI
2500 cas_mask_intr(cp);
Ben Hutchings288379f2009-01-19 16:43:59 -08002501 napi_schedule(&cp->napi);
David S. Miller1f26dac2005-09-27 15:24:13 -07002502#else
2503 cas_rx_ringN(cp, 1, 0);
2504#endif
2505 status &= ~INTR_RX_DONE_ALT;
2506 }
2507 if (status)
2508 cas_handle_irq1(cp, status);
2509 spin_unlock_irqrestore(&cp->lock, flags);
2510 return IRQ_HANDLED;
2511}
2512#endif
2513
2514static inline void cas_handle_irq(struct net_device *dev,
2515 struct cas *cp, const u32 status)
2516{
2517 /* housekeeping interrupts */
2518 if (status & INTR_ERROR_MASK)
2519 cas_abnormal_irq(dev, cp, status);
2520
2521 if (status & INTR_RX_BUF_UNAVAIL) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002522 /* Frame arrived, no free RX buffers available.
David S. Miller1f26dac2005-09-27 15:24:13 -07002523 * NOTE: we can get this on a link transition.
2524 */
2525 cas_post_rxds_ringN(cp, 0, 0);
2526 spin_lock(&cp->stat_lock[0]);
2527 cp->net_stats[0].rx_dropped++;
2528 spin_unlock(&cp->stat_lock[0]);
2529 } else if (status & INTR_RX_BUF_AE) {
2530 cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
2531 RX_AE_FREEN_VAL(0));
2532 }
2533
2534 if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
2535 cas_post_rxcs_ringN(dev, cp, 0);
2536}
2537
David Howells7d12e782006-10-05 14:55:46 +01002538static irqreturn_t cas_interrupt(int irq, void *dev_id)
David S. Miller1f26dac2005-09-27 15:24:13 -07002539{
2540 struct net_device *dev = dev_id;
2541 struct cas *cp = netdev_priv(dev);
2542 unsigned long flags;
2543 u32 status = readl(cp->regs + REG_INTR_STATUS);
2544
2545 if (status == 0)
2546 return IRQ_NONE;
2547
2548 spin_lock_irqsave(&cp->lock, flags);
2549 if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
2550 cas_tx(dev, cp, status);
2551 status &= ~(INTR_TX_ALL | INTR_TX_INTME);
2552 }
2553
2554 if (status & INTR_RX_DONE) {
2555#ifdef USE_NAPI
2556 cas_mask_intr(cp);
Ben Hutchings288379f2009-01-19 16:43:59 -08002557 napi_schedule(&cp->napi);
David S. Miller1f26dac2005-09-27 15:24:13 -07002558#else
2559 cas_rx_ringN(cp, 0, 0);
2560#endif
2561 status &= ~INTR_RX_DONE;
2562 }
2563
2564 if (status)
2565 cas_handle_irq(dev, cp, status);
2566 spin_unlock_irqrestore(&cp->lock, flags);
2567 return IRQ_HANDLED;
2568}
2569
2570
2571#ifdef USE_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002572static int cas_poll(struct napi_struct *napi, int budget)
David S. Miller1f26dac2005-09-27 15:24:13 -07002573{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002574 struct cas *cp = container_of(napi, struct cas, napi);
2575 struct net_device *dev = cp->dev;
David S. Miller86216262008-01-04 00:23:18 -08002576 int i, enable_intr, credits;
David S. Miller1f26dac2005-09-27 15:24:13 -07002577 u32 status = readl(cp->regs + REG_INTR_STATUS);
2578 unsigned long flags;
2579
2580 spin_lock_irqsave(&cp->lock, flags);
2581 cas_tx(dev, cp, status);
2582 spin_unlock_irqrestore(&cp->lock, flags);
2583
2584 /* NAPI rx packets. we spread the credits across all of the
2585 * rxc rings
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002586 *
2587 * to make sure we're fair with the work we loop through each
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002588 * ring N_RX_COMP_RING times with a request of
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002589 * budget / N_RX_COMP_RINGS
David S. Miller1f26dac2005-09-27 15:24:13 -07002590 */
2591 enable_intr = 1;
2592 credits = 0;
2593 for (i = 0; i < N_RX_COMP_RINGS; i++) {
2594 int j;
2595 for (j = 0; j < N_RX_COMP_RINGS; j++) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002596 credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
2597 if (credits >= budget) {
David S. Miller1f26dac2005-09-27 15:24:13 -07002598 enable_intr = 0;
2599 goto rx_comp;
2600 }
2601 }
2602 }
2603
2604rx_comp:
David S. Miller1f26dac2005-09-27 15:24:13 -07002605 /* final rx completion */
2606 spin_lock_irqsave(&cp->lock, flags);
2607 if (status)
2608 cas_handle_irq(dev, cp, status);
2609
2610#ifdef USE_PCI_INTB
2611 if (N_RX_COMP_RINGS > 1) {
2612 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
2613 if (status)
2614 cas_handle_irq1(dev, cp, status);
2615 }
2616#endif
2617
2618#ifdef USE_PCI_INTC
2619 if (N_RX_COMP_RINGS > 2) {
2620 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
2621 if (status)
2622 cas_handle_irqN(dev, cp, status, 2);
2623 }
2624#endif
2625
2626#ifdef USE_PCI_INTD
2627 if (N_RX_COMP_RINGS > 3) {
2628 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
2629 if (status)
2630 cas_handle_irqN(dev, cp, status, 3);
2631 }
2632#endif
2633 spin_unlock_irqrestore(&cp->lock, flags);
2634 if (enable_intr) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002635 napi_complete(napi);
David S. Miller1f26dac2005-09-27 15:24:13 -07002636 cas_unmask_intr(cp);
David S. Miller1f26dac2005-09-27 15:24:13 -07002637 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002638 return credits;
David S. Miller1f26dac2005-09-27 15:24:13 -07002639}
2640#endif
2641
2642#ifdef CONFIG_NET_POLL_CONTROLLER
2643static void cas_netpoll(struct net_device *dev)
2644{
2645 struct cas *cp = netdev_priv(dev);
2646
2647 cas_disable_irq(cp, 0);
David Howells7d12e782006-10-05 14:55:46 +01002648 cas_interrupt(cp->pdev->irq, dev);
David S. Miller1f26dac2005-09-27 15:24:13 -07002649 cas_enable_irq(cp, 0);
2650
2651#ifdef USE_PCI_INTB
2652 if (N_RX_COMP_RINGS > 1) {
2653 /* cas_interrupt1(); */
2654 }
2655#endif
2656#ifdef USE_PCI_INTC
2657 if (N_RX_COMP_RINGS > 2) {
2658 /* cas_interruptN(); */
2659 }
2660#endif
2661#ifdef USE_PCI_INTD
2662 if (N_RX_COMP_RINGS > 3) {
2663 /* cas_interruptN(); */
2664 }
2665#endif
2666}
2667#endif
2668
2669static void cas_tx_timeout(struct net_device *dev)
2670{
2671 struct cas *cp = netdev_priv(dev);
2672
Joe Perches436d27d2010-02-17 15:01:53 +00002673 netdev_err(dev, "transmit timed out, resetting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07002674 if (!cp->hw_running) {
Joe Perches436d27d2010-02-17 15:01:53 +00002675 netdev_err(dev, "hrm.. hw not running!\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07002676 return;
2677 }
2678
Joe Perches436d27d2010-02-17 15:01:53 +00002679 netdev_err(dev, "MIF_STATE[%08x]\n",
2680 readl(cp->regs + REG_MIF_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -07002681
Joe Perches436d27d2010-02-17 15:01:53 +00002682 netdev_err(dev, "MAC_STATE[%08x]\n",
2683 readl(cp->regs + REG_MAC_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -07002684
Joe Perches436d27d2010-02-17 15:01:53 +00002685 netdev_err(dev, "TX_STATE[%08x:%08x:%08x] FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
2686 readl(cp->regs + REG_TX_CFG),
2687 readl(cp->regs + REG_MAC_TX_STATUS),
2688 readl(cp->regs + REG_MAC_TX_CFG),
2689 readl(cp->regs + REG_TX_FIFO_PKT_CNT),
2690 readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
2691 readl(cp->regs + REG_TX_FIFO_READ_PTR),
2692 readl(cp->regs + REG_TX_SM_1),
2693 readl(cp->regs + REG_TX_SM_2));
David S. Miller1f26dac2005-09-27 15:24:13 -07002694
Joe Perches436d27d2010-02-17 15:01:53 +00002695 netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
2696 readl(cp->regs + REG_RX_CFG),
2697 readl(cp->regs + REG_MAC_RX_STATUS),
2698 readl(cp->regs + REG_MAC_RX_CFG));
David S. Miller1f26dac2005-09-27 15:24:13 -07002699
Joe Perches436d27d2010-02-17 15:01:53 +00002700 netdev_err(dev, "HP_STATE[%08x:%08x:%08x:%08x]\n",
2701 readl(cp->regs + REG_HP_STATE_MACHINE),
2702 readl(cp->regs + REG_HP_STATUS0),
2703 readl(cp->regs + REG_HP_STATUS1),
2704 readl(cp->regs + REG_HP_STATUS2));
David S. Miller1f26dac2005-09-27 15:24:13 -07002705
2706#if 1
2707 atomic_inc(&cp->reset_task_pending);
2708 atomic_inc(&cp->reset_task_pending_all);
2709 schedule_work(&cp->reset_task);
2710#else
2711 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
2712 schedule_work(&cp->reset_task);
2713#endif
2714}
2715
2716static inline int cas_intme(int ring, int entry)
2717{
2718 /* Algorithm: IRQ every 1/2 of descriptors. */
2719 if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
2720 return 1;
2721 return 0;
2722}
2723
2724
2725static void cas_write_txd(struct cas *cp, int ring, int entry,
2726 dma_addr_t mapping, int len, u64 ctrl, int last)
2727{
2728 struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
2729
2730 ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
2731 if (cas_intme(ring, entry))
2732 ctrl |= TX_DESC_INTME;
2733 if (last)
2734 ctrl |= TX_DESC_EOF;
2735 txd->control = cpu_to_le64(ctrl);
2736 txd->buffer = cpu_to_le64(mapping);
2737}
2738
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002739static inline void *tx_tiny_buf(struct cas *cp, const int ring,
David S. Miller1f26dac2005-09-27 15:24:13 -07002740 const int entry)
2741{
2742 return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
2743}
2744
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002745static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
David S. Miller1f26dac2005-09-27 15:24:13 -07002746 const int entry, const int tentry)
2747{
2748 cp->tx_tiny_use[ring][tentry].nbufs++;
2749 cp->tx_tiny_use[ring][entry].used = 1;
2750 return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
2751}
2752
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002753static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
David S. Miller1f26dac2005-09-27 15:24:13 -07002754 struct sk_buff *skb)
2755{
2756 struct net_device *dev = cp->dev;
2757 int entry, nr_frags, frag, tabort, tentry;
2758 dma_addr_t mapping;
2759 unsigned long flags;
2760 u64 ctrl;
2761 u32 len;
2762
2763 spin_lock_irqsave(&cp->tx_lock[ring], flags);
2764
2765 /* This is a hard error, log it. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002766 if (TX_BUFFS_AVAIL(cp, ring) <=
David S. Miller1f26dac2005-09-27 15:24:13 -07002767 CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
2768 netif_stop_queue(dev);
2769 spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
Joe Perches436d27d2010-02-17 15:01:53 +00002770 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07002771 return 1;
2772 }
2773
2774 ctrl = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07002775 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002776 const u64 csum_start_off = skb_checksum_start_offset(skb);
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002777 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
David S. Miller1f26dac2005-09-27 15:24:13 -07002778
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002779 ctrl = TX_DESC_CSUM_EN |
David S. Miller1f26dac2005-09-27 15:24:13 -07002780 CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
2781 CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
2782 }
2783
2784 entry = cp->tx_new[ring];
2785 cp->tx_skbs[ring][entry] = skb;
2786
2787 nr_frags = skb_shinfo(skb)->nr_frags;
2788 len = skb_headlen(skb);
2789 mapping = pci_map_page(cp->pdev, virt_to_page(skb->data),
2790 offset_in_page(skb->data), len,
2791 PCI_DMA_TODEVICE);
2792
2793 tentry = entry;
2794 tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
2795 if (unlikely(tabort)) {
2796 /* NOTE: len is always > tabort */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002797 cas_write_txd(cp, ring, entry, mapping, len - tabort,
David S. Miller1f26dac2005-09-27 15:24:13 -07002798 ctrl | TX_DESC_SOF, 0);
2799 entry = TX_DESC_NEXT(ring, entry);
2800
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002801 skb_copy_from_linear_data_offset(skb, len - tabort,
2802 tx_tiny_buf(cp, ring, entry), tabort);
David S. Miller1f26dac2005-09-27 15:24:13 -07002803 mapping = tx_tiny_map(cp, ring, entry, tentry);
2804 cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
2805 (nr_frags == 0));
2806 } else {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002807 cas_write_txd(cp, ring, entry, mapping, len, ctrl |
David S. Miller1f26dac2005-09-27 15:24:13 -07002808 TX_DESC_SOF, (nr_frags == 0));
2809 }
2810 entry = TX_DESC_NEXT(ring, entry);
2811
2812 for (frag = 0; frag < nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002813 const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
David S. Miller1f26dac2005-09-27 15:24:13 -07002814
Eric Dumazet9e903e02011-10-18 21:00:24 +00002815 len = skb_frag_size(fragp);
Ian Campbell18324d692011-08-29 23:18:25 +00002816 mapping = skb_frag_dma_map(&cp->pdev->dev, fragp, 0, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002817 DMA_TO_DEVICE);
David S. Miller1f26dac2005-09-27 15:24:13 -07002818
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002819 tabort = cas_calc_tabort(cp, skb_frag_off(fragp), len);
David S. Miller1f26dac2005-09-27 15:24:13 -07002820 if (unlikely(tabort)) {
2821 void *addr;
2822
2823 /* NOTE: len is always > tabort */
2824 cas_write_txd(cp, ring, entry, mapping, len - tabort,
2825 ctrl, 0);
2826 entry = TX_DESC_NEXT(ring, entry);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002827
Ian Campbell18324d692011-08-29 23:18:25 +00002828 addr = cas_page_map(skb_frag_page(fragp));
David S. Miller1f26dac2005-09-27 15:24:13 -07002829 memcpy(tx_tiny_buf(cp, ring, entry),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002830 addr + skb_frag_off(fragp) + len - tabort,
David S. Miller1f26dac2005-09-27 15:24:13 -07002831 tabort);
2832 cas_page_unmap(addr);
2833 mapping = tx_tiny_map(cp, ring, entry, tentry);
2834 len = tabort;
2835 }
2836
2837 cas_write_txd(cp, ring, entry, mapping, len, ctrl,
2838 (frag + 1 == nr_frags));
2839 entry = TX_DESC_NEXT(ring, entry);
2840 }
2841
2842 cp->tx_new[ring] = entry;
2843 if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
2844 netif_stop_queue(dev);
2845
Joe Perches436d27d2010-02-17 15:01:53 +00002846 netif_printk(cp, tx_queued, KERN_DEBUG, dev,
2847 "tx[%d] queued, slot %d, skblen %d, avail %d\n",
2848 ring, entry, skb->len, TX_BUFFS_AVAIL(cp, ring));
David S. Miller1f26dac2005-09-27 15:24:13 -07002849 writel(entry, cp->regs + REG_TX_KICKN(ring));
2850 spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
2851 return 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002852}
David S. Miller1f26dac2005-09-27 15:24:13 -07002853
Stephen Hemminger613573252009-08-31 19:50:58 +00002854static netdev_tx_t cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
David S. Miller1f26dac2005-09-27 15:24:13 -07002855{
2856 struct cas *cp = netdev_priv(dev);
2857
2858 /* this is only used as a load-balancing hint, so it doesn't
2859 * need to be SMP safe
2860 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002861 static int ring;
David S. Miller1f26dac2005-09-27 15:24:13 -07002862
Herbert Xu5b057c62006-06-23 02:06:41 -07002863 if (skb_padto(skb, cp->min_frame_size))
Patrick McHardy6ed10652009-06-23 06:03:08 +00002864 return NETDEV_TX_OK;
David S. Miller1f26dac2005-09-27 15:24:13 -07002865
2866 /* XXX: we need some higher-level QoS hooks to steer packets to
2867 * individual queues.
2868 */
2869 if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
Patrick McHardy5b548142009-06-12 06:22:29 +00002870 return NETDEV_TX_BUSY;
Patrick McHardy6ed10652009-06-23 06:03:08 +00002871 return NETDEV_TX_OK;
David S. Miller1f26dac2005-09-27 15:24:13 -07002872}
2873
2874static void cas_init_tx_dma(struct cas *cp)
2875{
2876 u64 desc_dma = cp->block_dvma;
2877 unsigned long off;
2878 u32 val;
2879 int i;
2880
2881 /* set up tx completion writeback registers. must be 8-byte aligned */
2882#ifdef USE_TX_COMPWB
2883 off = offsetof(struct cas_init_block, tx_compwb);
2884 writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
2885 writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
2886#endif
2887
2888 /* enable completion writebacks, enable paced mode,
2889 * disable read pipe, and disable pre-interrupt compwbs
2890 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002891 val = TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
David S. Miller1f26dac2005-09-27 15:24:13 -07002892 TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002893 TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
David S. Miller1f26dac2005-09-27 15:24:13 -07002894 TX_CFG_INTR_COMPWB_DIS;
2895
2896 /* write out tx ring info and tx desc bases */
2897 for (i = 0; i < MAX_TX_RINGS; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002898 off = (unsigned long) cp->init_txds[i] -
David S. Miller1f26dac2005-09-27 15:24:13 -07002899 (unsigned long) cp->init_block;
2900
2901 val |= CAS_TX_RINGN_BASE(i);
2902 writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
2903 writel((desc_dma + off) & 0xffffffff, cp->regs +
2904 REG_TX_DBN_LOW(i));
2905 /* don't zero out the kick register here as the system
2906 * will wedge
2907 */
2908 }
2909 writel(val, cp->regs + REG_TX_CFG);
2910
2911 /* program max burst sizes. these numbers should be different
2912 * if doing QoS.
2913 */
2914#ifdef USE_QOS
2915 writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2916 writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
2917 writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
2918 writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
2919#else
2920 writel(0x800, cp->regs + REG_TX_MAXBURST_0);
2921 writel(0x800, cp->regs + REG_TX_MAXBURST_1);
2922 writel(0x800, cp->regs + REG_TX_MAXBURST_2);
2923 writel(0x800, cp->regs + REG_TX_MAXBURST_3);
2924#endif
2925}
2926
2927/* Must be invoked under cp->lock. */
2928static inline void cas_init_dma(struct cas *cp)
2929{
2930 cas_init_tx_dma(cp);
2931 cas_init_rx_dma(cp);
2932}
2933
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002934static void cas_process_mc_list(struct cas *cp)
2935{
2936 u16 hash_table[16];
2937 u32 crc;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002938 struct netdev_hw_addr *ha;
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002939 int i = 1;
2940
2941 memset(hash_table, 0, sizeof(hash_table));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002942 netdev_for_each_mc_addr(ha, cp->dev) {
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002943 if (i <= CAS_MC_EXACT_MATCH_SIZE) {
2944 /* use the alternate mac address registers for the
2945 * first 15 multicast addresses
2946 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00002947 writel((ha->addr[4] << 8) | ha->addr[5],
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002948 cp->regs + REG_MAC_ADDRN(i*3 + 0));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002949 writel((ha->addr[2] << 8) | ha->addr[3],
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002950 cp->regs + REG_MAC_ADDRN(i*3 + 1));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002951 writel((ha->addr[0] << 8) | ha->addr[1],
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002952 cp->regs + REG_MAC_ADDRN(i*3 + 2));
2953 i++;
2954 }
2955 else {
2956 /* use hw hash table for the next series of
2957 * multicast addresses
2958 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00002959 crc = ether_crc_le(ETH_ALEN, ha->addr);
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002960 crc >>= 24;
2961 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
2962 }
2963 }
2964 for (i = 0; i < 16; i++)
2965 writel(hash_table[i], cp->regs + REG_MAC_HASH_TABLEN(i));
2966}
2967
David S. Miller1f26dac2005-09-27 15:24:13 -07002968/* Must be invoked under cp->lock. */
2969static u32 cas_setup_multicast(struct cas *cp)
2970{
2971 u32 rxcfg = 0;
2972 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002973
David S. Miller1f26dac2005-09-27 15:24:13 -07002974 if (cp->dev->flags & IFF_PROMISC) {
2975 rxcfg |= MAC_RX_CFG_PROMISC_EN;
2976
2977 } else if (cp->dev->flags & IFF_ALLMULTI) {
2978 for (i=0; i < 16; i++)
2979 writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
2980 rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2981
2982 } else {
Jiri Pirkod7b855c2010-02-17 10:43:33 +00002983 cas_process_mc_list(cp);
David S. Miller1f26dac2005-09-27 15:24:13 -07002984 rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
2985 }
2986
2987 return rxcfg;
2988}
2989
2990/* must be invoked under cp->stat_lock[N_TX_RINGS] */
2991static void cas_clear_mac_err(struct cas *cp)
2992{
2993 writel(0, cp->regs + REG_MAC_COLL_NORMAL);
2994 writel(0, cp->regs + REG_MAC_COLL_FIRST);
2995 writel(0, cp->regs + REG_MAC_COLL_EXCESS);
2996 writel(0, cp->regs + REG_MAC_COLL_LATE);
2997 writel(0, cp->regs + REG_MAC_TIMER_DEFER);
2998 writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
2999 writel(0, cp->regs + REG_MAC_RECV_FRAME);
3000 writel(0, cp->regs + REG_MAC_LEN_ERR);
3001 writel(0, cp->regs + REG_MAC_ALIGN_ERR);
3002 writel(0, cp->regs + REG_MAC_FCS_ERR);
3003 writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
3004}
3005
3006
3007static void cas_mac_reset(struct cas *cp)
3008{
3009 int i;
3010
3011 /* do both TX and RX reset */
3012 writel(0x1, cp->regs + REG_MAC_TX_RESET);
3013 writel(0x1, cp->regs + REG_MAC_RX_RESET);
3014
3015 /* wait for TX */
3016 i = STOP_TRIES;
3017 while (i-- > 0) {
3018 if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
3019 break;
3020 udelay(10);
3021 }
3022
3023 /* wait for RX */
3024 i = STOP_TRIES;
3025 while (i-- > 0) {
3026 if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
3027 break;
3028 udelay(10);
3029 }
3030
3031 if (readl(cp->regs + REG_MAC_TX_RESET) |
3032 readl(cp->regs + REG_MAC_RX_RESET))
Joe Perches436d27d2010-02-17 15:01:53 +00003033 netdev_err(cp->dev, "mac tx[%d]/rx[%d] reset failed [%08x]\n",
3034 readl(cp->regs + REG_MAC_TX_RESET),
3035 readl(cp->regs + REG_MAC_RX_RESET),
3036 readl(cp->regs + REG_MAC_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -07003037}
3038
3039
3040/* Must be invoked under cp->lock. */
3041static void cas_init_mac(struct cas *cp)
3042{
3043 unsigned char *e = &cp->dev->dev_addr[0];
3044 int i;
David S. Miller1f26dac2005-09-27 15:24:13 -07003045 cas_mac_reset(cp);
3046
3047 /* setup core arbitration weight register */
3048 writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
3049
David S. Miller1f26dac2005-09-27 15:24:13 -07003050#if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
3051 /* set the infinite burst register for chips that don't have
3052 * pci issues.
3053 */
3054 if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
3055 writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
3056#endif
3057
3058 writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
3059
3060 writel(0x00, cp->regs + REG_MAC_IPG0);
3061 writel(0x08, cp->regs + REG_MAC_IPG1);
3062 writel(0x04, cp->regs + REG_MAC_IPG2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003063
David S. Miller1f26dac2005-09-27 15:24:13 -07003064 /* change later for 802.3z */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003065 writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
David S. Miller1f26dac2005-09-27 15:24:13 -07003066
3067 /* min frame + FCS */
3068 writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
3069
3070 /* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003071 * specify the maximum frame size to prevent RX tag errors on
David S. Miller1f26dac2005-09-27 15:24:13 -07003072 * oversized frames.
3073 */
3074 writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003075 CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
3076 (CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
David S. Miller1f26dac2005-09-27 15:24:13 -07003077 cp->regs + REG_MAC_FRAMESIZE_MAX);
3078
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003079 /* NOTE: crc_size is used as a surrogate for half-duplex.
David S. Miller1f26dac2005-09-27 15:24:13 -07003080 * workaround saturn half-duplex issue by increasing preamble
3081 * size to 65 bytes.
3082 */
3083 if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
3084 writel(0x41, cp->regs + REG_MAC_PA_SIZE);
3085 else
3086 writel(0x07, cp->regs + REG_MAC_PA_SIZE);
3087 writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
3088 writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
3089 writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
3090
3091 writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
3092
3093 writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
3094 writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
3095 writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
3096 writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
3097 writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
3098
3099 /* setup mac address in perfect filter array */
3100 for (i = 0; i < 45; i++)
3101 writel(0x0, cp->regs + REG_MAC_ADDRN(i));
3102
3103 writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
3104 writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
3105 writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
3106
3107 writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
3108 writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
3109 writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
3110
David S. Miller1f26dac2005-09-27 15:24:13 -07003111 cp->mac_rx_cfg = cas_setup_multicast(cp);
Christoph Eggerff085462010-07-14 13:35:45 -07003112
David S. Miller1f26dac2005-09-27 15:24:13 -07003113 spin_lock(&cp->stat_lock[N_TX_RINGS]);
3114 cas_clear_mac_err(cp);
3115 spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3116
3117 /* Setup MAC interrupts. We want to get all of the interesting
3118 * counter expiration events, but we do not want to hear about
3119 * normal rx/tx as the DMA engine tells us that.
3120 */
3121 writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
3122 writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
3123
3124 /* Don't enable even the PAUSE interrupts for now, we
3125 * make no use of those events other than to record them.
3126 */
3127 writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
3128}
3129
3130/* Must be invoked under cp->lock. */
3131static void cas_init_pause_thresholds(struct cas *cp)
3132{
3133 /* Calculate pause thresholds. Setting the OFF threshold to the
3134 * full RX fifo size effectively disables PAUSE generation
3135 */
3136 if (cp->rx_fifo_size <= (2 * 1024)) {
3137 cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
3138 } else {
3139 int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
3140 if (max_frame * 3 > cp->rx_fifo_size) {
3141 cp->rx_pause_off = 7104;
3142 cp->rx_pause_on = 960;
3143 } else {
3144 int off = (cp->rx_fifo_size - (max_frame * 2));
3145 int on = off - max_frame;
3146 cp->rx_pause_off = off;
3147 cp->rx_pause_on = on;
3148 }
3149 }
3150}
3151
3152static int cas_vpd_match(const void __iomem *p, const char *str)
3153{
3154 int len = strlen(str) + 1;
3155 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003156
David S. Miller1f26dac2005-09-27 15:24:13 -07003157 for (i = 0; i < len; i++) {
3158 if (readb(p + i) != str[i])
3159 return 0;
3160 }
3161 return 1;
3162}
3163
3164
3165/* get the mac address by reading the vpd information in the rom.
3166 * also get the phy type and determine if there's an entropy generator.
3167 * NOTE: this is a bit convoluted for the following reasons:
3168 * 1) vpd info has order-dependent mac addresses for multinic cards
3169 * 2) the only way to determine the nic order is to use the slot
3170 * number.
3171 * 3) fiber cards don't have bridges, so their slot numbers don't
3172 * mean anything.
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003173 * 4) we don't actually know we have a fiber card until after
David S. Miller1f26dac2005-09-27 15:24:13 -07003174 * the mac addresses are parsed.
3175 */
3176static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
3177 const int offset)
3178{
3179 void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
3180 void __iomem *base, *kstart;
3181 int i, len;
3182 int found = 0;
3183#define VPD_FOUND_MAC 0x01
3184#define VPD_FOUND_PHY 0x02
3185
3186 int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
3187 int mac_off = 0;
3188
David S. Miller92d76e82011-01-14 12:39:59 -08003189#if defined(CONFIG_SPARC)
Richard Mortimer4e3dbdb2011-01-06 11:50:30 -08003190 const unsigned char *addr;
3191#endif
3192
David S. Miller1f26dac2005-09-27 15:24:13 -07003193 /* give us access to the PROM */
3194 writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
3195 cp->regs + REG_BIM_LOCAL_DEV_EN);
3196
3197 /* check for an expansion rom */
3198 if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
3199 goto use_random_mac_addr;
3200
3201 /* search for beginning of vpd */
Al Viro46d70312005-09-30 03:21:45 +01003202 base = NULL;
David S. Miller1f26dac2005-09-27 15:24:13 -07003203 for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
3204 /* check for PCIR */
3205 if ((readb(p + i + 0) == 0x50) &&
3206 (readb(p + i + 1) == 0x43) &&
3207 (readb(p + i + 2) == 0x49) &&
3208 (readb(p + i + 3) == 0x52)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003209 base = p + (readb(p + i + 8) |
David S. Miller1f26dac2005-09-27 15:24:13 -07003210 (readb(p + i + 9) << 8));
3211 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003212 }
David S. Miller1f26dac2005-09-27 15:24:13 -07003213 }
3214
3215 if (!base || (readb(base) != 0x82))
3216 goto use_random_mac_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003217
David S. Miller1f26dac2005-09-27 15:24:13 -07003218 i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
3219 while (i < EXPANSION_ROM_SIZE) {
3220 if (readb(base + i) != 0x90) /* no vpd found */
3221 goto use_random_mac_addr;
3222
3223 /* found a vpd field */
3224 len = readb(base + i + 1) | (readb(base + i + 2) << 8);
3225
3226 /* extract keywords */
3227 kstart = base + i + 3;
3228 p = kstart;
3229 while ((p - kstart) < len) {
3230 int klen = readb(p + 2);
3231 int j;
3232 char type;
3233
3234 p += 3;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003235
David S. Miller1f26dac2005-09-27 15:24:13 -07003236 /* look for the following things:
3237 * -- correct length == 29
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003238 * 3 (type) + 2 (size) +
3239 * 18 (strlen("local-mac-address") + 1) +
3240 * 6 (mac addr)
David S. Miller1f26dac2005-09-27 15:24:13 -07003241 * -- VPD Instance 'I'
3242 * -- VPD Type Bytes 'B'
3243 * -- VPD data length == 6
3244 * -- property string == local-mac-address
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003245 *
David S. Miller1f26dac2005-09-27 15:24:13 -07003246 * -- correct length == 24
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003247 * 3 (type) + 2 (size) +
3248 * 12 (strlen("entropy-dev") + 1) +
David S. Miller1f26dac2005-09-27 15:24:13 -07003249 * 7 (strlen("vms110") + 1)
3250 * -- VPD Instance 'I'
3251 * -- VPD Type String 'B'
3252 * -- VPD data length == 7
3253 * -- property string == entropy-dev
3254 *
3255 * -- correct length == 18
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003256 * 3 (type) + 2 (size) +
3257 * 9 (strlen("phy-type") + 1) +
David S. Miller1f26dac2005-09-27 15:24:13 -07003258 * 4 (strlen("pcs") + 1)
3259 * -- VPD Instance 'I'
3260 * -- VPD Type String 'S'
3261 * -- VPD data length == 4
3262 * -- property string == phy-type
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003263 *
David S. Miller1f26dac2005-09-27 15:24:13 -07003264 * -- correct length == 23
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003265 * 3 (type) + 2 (size) +
3266 * 14 (strlen("phy-interface") + 1) +
David S. Miller1f26dac2005-09-27 15:24:13 -07003267 * 4 (strlen("pcs") + 1)
3268 * -- VPD Instance 'I'
3269 * -- VPD Type String 'S'
3270 * -- VPD data length == 4
3271 * -- property string == phy-interface
3272 */
3273 if (readb(p) != 'I')
3274 goto next;
3275
3276 /* finally, check string and length */
3277 type = readb(p + 3);
3278 if (type == 'B') {
3279 if ((klen == 29) && readb(p + 4) == 6 &&
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003280 cas_vpd_match(p + 5,
David S. Miller1f26dac2005-09-27 15:24:13 -07003281 "local-mac-address")) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003282 if (mac_off++ > offset)
David S. Miller1f26dac2005-09-27 15:24:13 -07003283 goto next;
3284
3285 /* set mac address */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003286 for (j = 0; j < 6; j++)
3287 dev_addr[j] =
David S. Miller1f26dac2005-09-27 15:24:13 -07003288 readb(p + 23 + j);
3289 goto found_mac;
3290 }
3291 }
3292
3293 if (type != 'S')
3294 goto next;
3295
3296#ifdef USE_ENTROPY_DEV
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003297 if ((klen == 24) &&
David S. Miller1f26dac2005-09-27 15:24:13 -07003298 cas_vpd_match(p + 5, "entropy-dev") &&
3299 cas_vpd_match(p + 17, "vms110")) {
3300 cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
3301 goto next;
3302 }
3303#endif
3304
3305 if (found & VPD_FOUND_PHY)
3306 goto next;
3307
3308 if ((klen == 18) && readb(p + 4) == 4 &&
3309 cas_vpd_match(p + 5, "phy-type")) {
3310 if (cas_vpd_match(p + 14, "pcs")) {
3311 phy_type = CAS_PHY_SERDES;
3312 goto found_phy;
3313 }
3314 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003315
David S. Miller1f26dac2005-09-27 15:24:13 -07003316 if ((klen == 23) && readb(p + 4) == 4 &&
3317 cas_vpd_match(p + 5, "phy-interface")) {
3318 if (cas_vpd_match(p + 19, "pcs")) {
3319 phy_type = CAS_PHY_SERDES;
3320 goto found_phy;
3321 }
3322 }
3323found_mac:
3324 found |= VPD_FOUND_MAC;
3325 goto next;
3326
3327found_phy:
3328 found |= VPD_FOUND_PHY;
3329
3330next:
3331 p += klen;
3332 }
3333 i += len + 3;
3334 }
3335
3336use_random_mac_addr:
3337 if (found & VPD_FOUND_MAC)
3338 goto done;
3339
David S. Miller92d76e82011-01-14 12:39:59 -08003340#if defined(CONFIG_SPARC)
Richard Mortimer4e3dbdb2011-01-06 11:50:30 -08003341 addr = of_get_property(cp->of_node, "local-mac-address", NULL);
3342 if (addr != NULL) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07003343 memcpy(dev_addr, addr, ETH_ALEN);
Richard Mortimer4e3dbdb2011-01-06 11:50:30 -08003344 goto done;
3345 }
3346#endif
3347
David S. Miller1f26dac2005-09-27 15:24:13 -07003348 /* Sun MAC prefix then 3 random bytes. */
Joe Perches436d27d2010-02-17 15:01:53 +00003349 pr_info("MAC address not found in ROM VPD\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07003350 dev_addr[0] = 0x08;
3351 dev_addr[1] = 0x00;
3352 dev_addr[2] = 0x20;
3353 get_random_bytes(dev_addr + 3, 3);
3354
3355done:
3356 writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
3357 return phy_type;
3358}
3359
3360/* check pci invariants */
3361static void cas_check_pci_invariants(struct cas *cp)
3362{
3363 struct pci_dev *pdev = cp->pdev;
David S. Miller1f26dac2005-09-27 15:24:13 -07003364
3365 cp->cas_flags = 0;
David S. Miller1f26dac2005-09-27 15:24:13 -07003366 if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
3367 (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
Auke Kok44c10132007-06-08 15:46:36 -07003368 if (pdev->revision >= CAS_ID_REVPLUS)
David S. Miller1f26dac2005-09-27 15:24:13 -07003369 cp->cas_flags |= CAS_FLAG_REG_PLUS;
Auke Kok44c10132007-06-08 15:46:36 -07003370 if (pdev->revision < CAS_ID_REVPLUS02u)
David S. Miller1f26dac2005-09-27 15:24:13 -07003371 cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
3372
3373 /* Original Cassini supports HW CSUM, but it's not
3374 * enabled by default as it can trigger TX hangs.
3375 */
Auke Kok44c10132007-06-08 15:46:36 -07003376 if (pdev->revision < CAS_ID_REV2)
David S. Miller1f26dac2005-09-27 15:24:13 -07003377 cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
3378 } else {
3379 /* Only sun has original cassini chips. */
3380 cp->cas_flags |= CAS_FLAG_REG_PLUS;
3381
3382 /* We use a flag because the same phy might be externally
3383 * connected.
3384 */
3385 if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
3386 (pdev->device == PCI_DEVICE_ID_NS_SATURN))
3387 cp->cas_flags |= CAS_FLAG_SATURN;
3388 }
3389}
3390
3391
3392static int cas_check_invariants(struct cas *cp)
3393{
3394 struct pci_dev *pdev = cp->pdev;
3395 u32 cfg;
3396 int i;
3397
3398 /* get page size for rx buffers. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003399 cp->page_order = 0;
David S. Miller1f26dac2005-09-27 15:24:13 -07003400#ifdef USE_PAGE_ORDER
3401 if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
3402 /* see if we can allocate larger pages */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003403 struct page *page = alloc_pages(GFP_ATOMIC,
3404 CAS_JUMBO_PAGE_SHIFT -
David S. Miller1f26dac2005-09-27 15:24:13 -07003405 PAGE_SHIFT);
3406 if (page) {
3407 __free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
3408 cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
3409 } else {
Joe Perches436d27d2010-02-17 15:01:53 +00003410 printk("MTU limited to %d bytes\n", CAS_MAX_MTU);
David S. Miller1f26dac2005-09-27 15:24:13 -07003411 }
3412 }
3413#endif
3414 cp->page_size = (PAGE_SIZE << cp->page_order);
3415
3416 /* Fetch the FIFO configurations. */
3417 cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
3418 cp->rx_fifo_size = RX_FIFO_SIZE;
3419
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003420 /* finish phy determination. MDIO1 takes precedence over MDIO0 if
David S. Miller1f26dac2005-09-27 15:24:13 -07003421 * they're both connected.
3422 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003423 cp->phy_type = cas_get_vpd_info(cp, cp->dev->dev_addr,
David S. Miller1f26dac2005-09-27 15:24:13 -07003424 PCI_SLOT(pdev->devfn));
3425 if (cp->phy_type & CAS_PHY_SERDES) {
3426 cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3427 return 0; /* no more checking needed */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003428 }
David S. Miller1f26dac2005-09-27 15:24:13 -07003429
3430 /* MII */
3431 cfg = readl(cp->regs + REG_MIF_CFG);
3432 if (cfg & MIF_CFG_MDIO_1) {
3433 cp->phy_type = CAS_PHY_MII_MDIO1;
3434 } else if (cfg & MIF_CFG_MDIO_0) {
3435 cp->phy_type = CAS_PHY_MII_MDIO0;
3436 }
3437
3438 cas_mif_poll(cp, 0);
3439 writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3440
3441 for (i = 0; i < 32; i++) {
3442 u32 phy_id;
3443 int j;
3444
3445 for (j = 0; j < 3; j++) {
3446 cp->phy_addr = i;
3447 phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
3448 phy_id |= cas_phy_read(cp, MII_PHYSID2);
3449 if (phy_id && (phy_id != 0xFFFFFFFF)) {
3450 cp->phy_id = phy_id;
3451 goto done;
3452 }
3453 }
3454 }
Joe Perches436d27d2010-02-17 15:01:53 +00003455 pr_err("MII phy did not respond [%08x]\n",
David S. Miller1f26dac2005-09-27 15:24:13 -07003456 readl(cp->regs + REG_MIF_STATE_MACHINE));
3457 return -1;
3458
3459done:
3460 /* see if we can do gigabit */
3461 cfg = cas_phy_read(cp, MII_BMSR);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003462 if ((cfg & CAS_BMSR_1000_EXTEND) &&
David S. Miller1f26dac2005-09-27 15:24:13 -07003463 cas_phy_read(cp, CAS_MII_1000_EXTEND))
3464 cp->cas_flags |= CAS_FLAG_1000MB_CAP;
3465 return 0;
3466}
3467
3468/* Must be invoked under cp->lock. */
3469static inline void cas_start_dma(struct cas *cp)
3470{
3471 int i;
3472 u32 val;
3473 int txfailed = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003474
David S. Miller1f26dac2005-09-27 15:24:13 -07003475 /* enable dma */
3476 val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
3477 writel(val, cp->regs + REG_TX_CFG);
3478 val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
3479 writel(val, cp->regs + REG_RX_CFG);
3480
3481 /* enable the mac */
3482 val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
3483 writel(val, cp->regs + REG_MAC_TX_CFG);
3484 val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
3485 writel(val, cp->regs + REG_MAC_RX_CFG);
3486
3487 i = STOP_TRIES;
3488 while (i-- > 0) {
3489 val = readl(cp->regs + REG_MAC_TX_CFG);
3490 if ((val & MAC_TX_CFG_EN))
3491 break;
3492 udelay(10);
3493 }
3494 if (i < 0) txfailed = 1;
3495 i = STOP_TRIES;
3496 while (i-- > 0) {
3497 val = readl(cp->regs + REG_MAC_RX_CFG);
3498 if ((val & MAC_RX_CFG_EN)) {
3499 if (txfailed) {
Joe Perches436d27d2010-02-17 15:01:53 +00003500 netdev_err(cp->dev,
3501 "enabling mac failed [tx:%08x:%08x]\n",
3502 readl(cp->regs + REG_MIF_STATE_MACHINE),
3503 readl(cp->regs + REG_MAC_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -07003504 }
3505 goto enable_rx_done;
3506 }
3507 udelay(10);
3508 }
Joe Perches436d27d2010-02-17 15:01:53 +00003509 netdev_err(cp->dev, "enabling mac failed [%s:%08x:%08x]\n",
3510 (txfailed ? "tx,rx" : "rx"),
3511 readl(cp->regs + REG_MIF_STATE_MACHINE),
3512 readl(cp->regs + REG_MAC_STATE_MACHINE));
David S. Miller1f26dac2005-09-27 15:24:13 -07003513
3514enable_rx_done:
3515 cas_unmask_intr(cp); /* enable interrupts */
3516 writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
3517 writel(0, cp->regs + REG_RX_COMP_TAIL);
3518
3519 if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003520 if (N_RX_DESC_RINGS > 1)
3521 writel(RX_DESC_RINGN_SIZE(1) - 4,
David S. Miller1f26dac2005-09-27 15:24:13 -07003522 cp->regs + REG_PLUS_RX_KICK1);
3523
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003524 for (i = 1; i < N_RX_COMP_RINGS; i++)
David S. Miller1f26dac2005-09-27 15:24:13 -07003525 writel(0, cp->regs + REG_PLUS_RX_COMPN_TAIL(i));
3526 }
3527}
3528
3529/* Must be invoked under cp->lock. */
3530static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
3531 int *pause)
3532{
3533 u32 val = readl(cp->regs + REG_PCS_MII_LPA);
3534 *fd = (val & PCS_MII_LPA_FD) ? 1 : 0;
3535 *pause = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
3536 if (val & PCS_MII_LPA_ASYM_PAUSE)
3537 *pause |= 0x10;
3538 *spd = 1000;
3539}
3540
3541/* Must be invoked under cp->lock. */
3542static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
3543 int *pause)
3544{
3545 u32 val;
3546
3547 *fd = 0;
3548 *spd = 10;
3549 *pause = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003550
David S. Miller1f26dac2005-09-27 15:24:13 -07003551 /* use GMII registers */
3552 val = cas_phy_read(cp, MII_LPA);
3553 if (val & CAS_LPA_PAUSE)
3554 *pause = 0x01;
3555
3556 if (val & CAS_LPA_ASYM_PAUSE)
3557 *pause |= 0x10;
3558
3559 if (val & LPA_DUPLEX)
3560 *fd = 1;
3561 if (val & LPA_100)
3562 *spd = 100;
3563
3564 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
3565 val = cas_phy_read(cp, CAS_MII_1000_STATUS);
3566 if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
3567 *spd = 1000;
3568 if (val & CAS_LPA_1000FULL)
3569 *fd = 1;
3570 }
3571}
3572
3573/* A link-up condition has occurred, initialize and enable the
3574 * rest of the chip.
3575 *
3576 * Must be invoked under cp->lock.
3577 */
3578static void cas_set_link_modes(struct cas *cp)
3579{
3580 u32 val;
3581 int full_duplex, speed, pause;
3582
3583 full_duplex = 0;
3584 speed = 10;
3585 pause = 0;
3586
3587 if (CAS_PHY_MII(cp->phy_type)) {
3588 cas_mif_poll(cp, 0);
3589 val = cas_phy_read(cp, MII_BMCR);
3590 if (val & BMCR_ANENABLE) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003591 cas_read_mii_link_mode(cp, &full_duplex, &speed,
David S. Miller1f26dac2005-09-27 15:24:13 -07003592 &pause);
3593 } else {
3594 if (val & BMCR_FULLDPLX)
3595 full_duplex = 1;
3596
3597 if (val & BMCR_SPEED100)
3598 speed = 100;
3599 else if (val & CAS_BMCR_SPEED1000)
3600 speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
3601 1000 : 100;
3602 }
3603 cas_mif_poll(cp, 1);
3604
3605 } else {
3606 val = readl(cp->regs + REG_PCS_MII_CTRL);
3607 cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
3608 if ((val & PCS_MII_AUTONEG_EN) == 0) {
3609 if (val & PCS_MII_CTRL_DUPLEX)
3610 full_duplex = 1;
3611 }
3612 }
3613
Joe Perches436d27d2010-02-17 15:01:53 +00003614 netif_info(cp, link, cp->dev, "Link up at %d Mbps, %s-duplex\n",
3615 speed, full_duplex ? "full" : "half");
David S. Miller1f26dac2005-09-27 15:24:13 -07003616
3617 val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
3618 if (CAS_PHY_MII(cp->phy_type)) {
3619 val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
3620 if (!full_duplex)
3621 val |= MAC_XIF_DISABLE_ECHO;
3622 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003623 if (full_duplex)
David S. Miller1f26dac2005-09-27 15:24:13 -07003624 val |= MAC_XIF_FDPLX_LED;
3625 if (speed == 1000)
3626 val |= MAC_XIF_GMII_MODE;
3627 writel(val, cp->regs + REG_MAC_XIF_CFG);
3628
3629 /* deal with carrier and collision detect. */
3630 val = MAC_TX_CFG_IPG_EN;
3631 if (full_duplex) {
3632 val |= MAC_TX_CFG_IGNORE_CARRIER;
3633 val |= MAC_TX_CFG_IGNORE_COLL;
3634 } else {
3635#ifndef USE_CSMA_CD_PROTO
3636 val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
3637 val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
3638#endif
3639 }
3640 /* val now set up for REG_MAC_TX_CFG */
3641
3642 /* If gigabit and half-duplex, enable carrier extension
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003643 * mode. increase slot time to 512 bytes as well.
David S. Miller1f26dac2005-09-27 15:24:13 -07003644 * else, disable it and make sure slot time is 64 bytes.
3645 * also activate checksum bug workaround
3646 */
3647 if ((speed == 1000) && !full_duplex) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003648 writel(val | MAC_TX_CFG_CARRIER_EXTEND,
David S. Miller1f26dac2005-09-27 15:24:13 -07003649 cp->regs + REG_MAC_TX_CFG);
3650
3651 val = readl(cp->regs + REG_MAC_RX_CFG);
3652 val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003653 writel(val | MAC_RX_CFG_CARRIER_EXTEND,
David S. Miller1f26dac2005-09-27 15:24:13 -07003654 cp->regs + REG_MAC_RX_CFG);
3655
3656 writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
3657
3658 cp->crc_size = 4;
3659 /* minimum size gigabit frame at half duplex */
3660 cp->min_frame_size = CAS_1000MB_MIN_FRAME;
3661
3662 } else {
3663 writel(val, cp->regs + REG_MAC_TX_CFG);
3664
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003665 /* checksum bug workaround. don't strip FCS when in
David S. Miller1f26dac2005-09-27 15:24:13 -07003666 * half-duplex mode
3667 */
3668 val = readl(cp->regs + REG_MAC_RX_CFG);
3669 if (full_duplex) {
3670 val |= MAC_RX_CFG_STRIP_FCS;
3671 cp->crc_size = 0;
3672 cp->min_frame_size = CAS_MIN_MTU;
3673 } else {
3674 val &= ~MAC_RX_CFG_STRIP_FCS;
3675 cp->crc_size = 4;
3676 cp->min_frame_size = CAS_MIN_FRAME;
3677 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003678 writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
David S. Miller1f26dac2005-09-27 15:24:13 -07003679 cp->regs + REG_MAC_RX_CFG);
3680 writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
3681 }
3682
3683 if (netif_msg_link(cp)) {
3684 if (pause & 0x01) {
Joe Perches436d27d2010-02-17 15:01:53 +00003685 netdev_info(cp->dev, "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
3686 cp->rx_fifo_size,
3687 cp->rx_pause_off,
3688 cp->rx_pause_on);
David S. Miller1f26dac2005-09-27 15:24:13 -07003689 } else if (pause & 0x10) {
Joe Perches436d27d2010-02-17 15:01:53 +00003690 netdev_info(cp->dev, "TX pause enabled\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07003691 } else {
Joe Perches436d27d2010-02-17 15:01:53 +00003692 netdev_info(cp->dev, "Pause is disabled\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07003693 }
3694 }
3695
3696 val = readl(cp->regs + REG_MAC_CTRL_CFG);
3697 val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
3698 if (pause) { /* symmetric or asymmetric pause */
3699 val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
3700 if (pause & 0x01) { /* symmetric pause */
3701 val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003702 }
David S. Miller1f26dac2005-09-27 15:24:13 -07003703 }
3704 writel(val, cp->regs + REG_MAC_CTRL_CFG);
3705 cas_start_dma(cp);
3706}
3707
3708/* Must be invoked under cp->lock. */
3709static void cas_init_hw(struct cas *cp, int restart_link)
3710{
3711 if (restart_link)
3712 cas_phy_init(cp);
3713
3714 cas_init_pause_thresholds(cp);
3715 cas_init_mac(cp);
3716 cas_init_dma(cp);
3717
3718 if (restart_link) {
3719 /* Default aneg parameters */
3720 cp->timer_ticks = 0;
3721 cas_begin_auto_negotiation(cp, NULL);
3722 } else if (cp->lstate == link_up) {
3723 cas_set_link_modes(cp);
3724 netif_carrier_on(cp->dev);
3725 }
3726}
3727
3728/* Must be invoked under cp->lock. on earlier cassini boards,
3729 * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
3730 * let it settle out, and then restore pci state.
3731 */
3732static void cas_hard_reset(struct cas *cp)
3733{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003734 writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
David S. Miller1f26dac2005-09-27 15:24:13 -07003735 udelay(20);
3736 pci_restore_state(cp->pdev);
3737}
3738
3739
3740static void cas_global_reset(struct cas *cp, int blkflag)
3741{
3742 int limit;
3743
3744 /* issue a global reset. don't use RSTOUT. */
3745 if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
3746 /* For PCS, when the blkflag is set, we should set the
3747 * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
3748 * the last autonegotiation from being cleared. We'll
3749 * need some special handling if the chip is set into a
3750 * loopback mode.
3751 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003752 writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
David S. Miller1f26dac2005-09-27 15:24:13 -07003753 cp->regs + REG_SW_RESET);
3754 } else {
3755 writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
3756 }
3757
3758 /* need to wait at least 3ms before polling register */
3759 mdelay(3);
3760
3761 limit = STOP_TRIES;
3762 while (limit-- > 0) {
3763 u32 val = readl(cp->regs + REG_SW_RESET);
3764 if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
3765 goto done;
3766 udelay(10);
3767 }
Joe Perches436d27d2010-02-17 15:01:53 +00003768 netdev_err(cp->dev, "sw reset failed\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07003769
3770done:
3771 /* enable various BIM interrupts */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003772 writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
David S. Miller1f26dac2005-09-27 15:24:13 -07003773 BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
3774
3775 /* clear out pci error status mask for handled errors.
3776 * we don't deal with DMA counter overflows as they happen
3777 * all the time.
3778 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003779 writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
3780 PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
3781 PCI_ERR_BIM_DMA_READ), cp->regs +
David S. Miller1f26dac2005-09-27 15:24:13 -07003782 REG_PCI_ERR_STATUS_MASK);
3783
3784 /* set up for MII by default to address mac rx reset timeout
3785 * issue
3786 */
3787 writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
3788}
3789
3790static void cas_reset(struct cas *cp, int blkflag)
3791{
3792 u32 val;
3793
3794 cas_mask_intr(cp);
3795 cas_global_reset(cp, blkflag);
3796 cas_mac_reset(cp);
3797 cas_entropy_reset(cp);
3798
3799 /* disable dma engines. */
3800 val = readl(cp->regs + REG_TX_CFG);
3801 val &= ~TX_CFG_DMA_EN;
3802 writel(val, cp->regs + REG_TX_CFG);
3803
3804 val = readl(cp->regs + REG_RX_CFG);
3805 val &= ~RX_CFG_DMA_EN;
3806 writel(val, cp->regs + REG_RX_CFG);
3807
3808 /* program header parser */
3809 if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
3810 (CAS_HP_ALT_FIRMWARE == cas_prog_null)) {
3811 cas_load_firmware(cp, CAS_HP_FIRMWARE);
3812 } else {
3813 cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
3814 }
3815
3816 /* clear out error registers */
3817 spin_lock(&cp->stat_lock[N_TX_RINGS]);
3818 cas_clear_mac_err(cp);
3819 spin_unlock(&cp->stat_lock[N_TX_RINGS]);
3820}
3821
Ingo Molnar758df692006-03-20 22:34:09 -08003822/* Shut down the chip, must be called with pm_mutex held. */
David S. Miller1f26dac2005-09-27 15:24:13 -07003823static void cas_shutdown(struct cas *cp)
3824{
3825 unsigned long flags;
3826
3827 /* Make us not-running to avoid timers respawning */
3828 cp->hw_running = 0;
3829
3830 del_timer_sync(&cp->link_timer);
3831
3832 /* Stop the reset task */
3833#if 0
3834 while (atomic_read(&cp->reset_task_pending_mtu) ||
3835 atomic_read(&cp->reset_task_pending_spare) ||
3836 atomic_read(&cp->reset_task_pending_all))
3837 schedule();
3838
3839#else
3840 while (atomic_read(&cp->reset_task_pending))
3841 schedule();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003842#endif
David S. Miller1f26dac2005-09-27 15:24:13 -07003843 /* Actually stop the chip */
3844 cas_lock_all_save(cp, flags);
3845 cas_reset(cp, 0);
3846 if (cp->cas_flags & CAS_FLAG_SATURN)
3847 cas_phy_powerdown(cp);
3848 cas_unlock_all_restore(cp, flags);
3849}
3850
3851static int cas_change_mtu(struct net_device *dev, int new_mtu)
3852{
3853 struct cas *cp = netdev_priv(dev);
3854
David S. Miller1f26dac2005-09-27 15:24:13 -07003855 dev->mtu = new_mtu;
3856 if (!netif_running(dev) || !netif_device_present(dev))
3857 return 0;
3858
3859 /* let the reset task handle it */
3860#if 1
3861 atomic_inc(&cp->reset_task_pending);
3862 if ((cp->phy_type & CAS_PHY_SERDES)) {
3863 atomic_inc(&cp->reset_task_pending_all);
3864 } else {
3865 atomic_inc(&cp->reset_task_pending_mtu);
3866 }
3867 schedule_work(&cp->reset_task);
3868#else
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003869 atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
David S. Miller1f26dac2005-09-27 15:24:13 -07003870 CAS_RESET_ALL : CAS_RESET_MTU);
Joe Perches436d27d2010-02-17 15:01:53 +00003871 pr_err("reset called in cas_change_mtu\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07003872 schedule_work(&cp->reset_task);
3873#endif
3874
Tejun Heo43829732012-08-20 14:51:24 -07003875 flush_work(&cp->reset_task);
David S. Miller1f26dac2005-09-27 15:24:13 -07003876 return 0;
3877}
3878
3879static void cas_clean_txd(struct cas *cp, int ring)
3880{
3881 struct cas_tx_desc *txd = cp->init_txds[ring];
3882 struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
3883 u64 daddr, dlen;
3884 int i, size;
3885
3886 size = TX_DESC_RINGN_SIZE(ring);
3887 for (i = 0; i < size; i++) {
3888 int frag;
3889
3890 if (skbs[i] == NULL)
3891 continue;
3892
3893 skb = skbs[i];
3894 skbs[i] = NULL;
3895
3896 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
3897 int ent = i & (size - 1);
3898
3899 /* first buffer is never a tiny buffer and so
3900 * needs to be unmapped.
3901 */
3902 daddr = le64_to_cpu(txd[ent].buffer);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003903 dlen = CAS_VAL(TX_DESC_BUFLEN,
David S. Miller1f26dac2005-09-27 15:24:13 -07003904 le64_to_cpu(txd[ent].control));
3905 pci_unmap_page(cp->pdev, daddr, dlen,
3906 PCI_DMA_TODEVICE);
3907
3908 if (frag != skb_shinfo(skb)->nr_frags) {
3909 i++;
3910
3911 /* next buffer might by a tiny buffer.
3912 * skip past it.
3913 */
3914 ent = i & (size - 1);
3915 if (cp->tx_tiny_use[ring][ent].used)
3916 i++;
3917 }
3918 }
3919 dev_kfree_skb_any(skb);
3920 }
3921
3922 /* zero out tiny buf usage */
3923 memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
3924}
3925
3926/* freed on close */
3927static inline void cas_free_rx_desc(struct cas *cp, int ring)
3928{
3929 cas_page_t **page = cp->rx_pages[ring];
3930 int i, size;
3931
3932 size = RX_DESC_RINGN_SIZE(ring);
3933 for (i = 0; i < size; i++) {
3934 if (page[i]) {
3935 cas_page_free(cp, page[i]);
3936 page[i] = NULL;
3937 }
3938 }
3939}
3940
3941static void cas_free_rxds(struct cas *cp)
3942{
3943 int i;
3944
3945 for (i = 0; i < N_RX_DESC_RINGS; i++)
3946 cas_free_rx_desc(cp, i);
3947}
3948
3949/* Must be invoked under cp->lock. */
3950static void cas_clean_rings(struct cas *cp)
3951{
3952 int i;
3953
3954 /* need to clean all tx rings */
3955 memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
3956 memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
3957 for (i = 0; i < N_TX_RINGS; i++)
3958 cas_clean_txd(cp, i);
3959
3960 /* zero out init block */
3961 memset(cp->init_block, 0, sizeof(struct cas_init_block));
3962 cas_clean_rxds(cp);
3963 cas_clean_rxcs(cp);
3964}
3965
3966/* allocated on open */
3967static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
3968{
3969 cas_page_t **page = cp->rx_pages[ring];
3970 int size, i = 0;
3971
3972 size = RX_DESC_RINGN_SIZE(ring);
3973 for (i = 0; i < size; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003974 if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
David S. Miller1f26dac2005-09-27 15:24:13 -07003975 return -1;
3976 }
3977 return 0;
3978}
3979
3980static int cas_alloc_rxds(struct cas *cp)
3981{
3982 int i;
3983
3984 for (i = 0; i < N_RX_DESC_RINGS; i++) {
3985 if (cas_alloc_rx_desc(cp, i) < 0) {
3986 cas_free_rxds(cp);
3987 return -1;
3988 }
3989 }
3990 return 0;
3991}
3992
David Howellsc4028952006-11-22 14:57:56 +00003993static void cas_reset_task(struct work_struct *work)
David S. Miller1f26dac2005-09-27 15:24:13 -07003994{
David Howellsc4028952006-11-22 14:57:56 +00003995 struct cas *cp = container_of(work, struct cas, reset_task);
David S. Miller1f26dac2005-09-27 15:24:13 -07003996#if 0
3997 int pending = atomic_read(&cp->reset_task_pending);
3998#else
3999 int pending_all = atomic_read(&cp->reset_task_pending_all);
4000 int pending_spare = atomic_read(&cp->reset_task_pending_spare);
4001 int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
4002
4003 if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
4004 /* We can have more tasks scheduled than actually
4005 * needed.
4006 */
4007 atomic_dec(&cp->reset_task_pending);
4008 return;
4009 }
4010#endif
4011 /* The link went down, we reset the ring, but keep
4012 * DMA stopped. Use this function for reset
4013 * on error as well.
4014 */
4015 if (cp->hw_running) {
4016 unsigned long flags;
4017
4018 /* Make sure we don't get interrupts or tx packets */
4019 netif_device_detach(cp->dev);
4020 cas_lock_all_save(cp, flags);
4021
4022 if (cp->opened) {
4023 /* We call cas_spare_recover when we call cas_open.
4024 * but we do not initialize the lists cas_spare_recover
4025 * uses until cas_open is called.
4026 */
4027 cas_spare_recover(cp, GFP_ATOMIC);
4028 }
4029#if 1
4030 /* test => only pending_spare set */
4031 if (!pending_all && !pending_mtu)
4032 goto done;
4033#else
4034 if (pending == CAS_RESET_SPARE)
4035 goto done;
4036#endif
4037 /* when pending == CAS_RESET_ALL, the following
4038 * call to cas_init_hw will restart auto negotiation.
4039 * Setting the second argument of cas_reset to
4040 * !(pending == CAS_RESET_ALL) will set this argument
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004041 * to 1 (avoiding reinitializing the PHY for the normal
David S. Miller1f26dac2005-09-27 15:24:13 -07004042 * PCS case) when auto negotiation is not restarted.
4043 */
4044#if 1
4045 cas_reset(cp, !(pending_all > 0));
4046 if (cp->opened)
4047 cas_clean_rings(cp);
4048 cas_init_hw(cp, (pending_all > 0));
4049#else
4050 cas_reset(cp, !(pending == CAS_RESET_ALL));
4051 if (cp->opened)
4052 cas_clean_rings(cp);
4053 cas_init_hw(cp, pending == CAS_RESET_ALL);
4054#endif
4055
4056done:
4057 cas_unlock_all_restore(cp, flags);
4058 netif_device_attach(cp->dev);
4059 }
4060#if 1
4061 atomic_sub(pending_all, &cp->reset_task_pending_all);
4062 atomic_sub(pending_spare, &cp->reset_task_pending_spare);
4063 atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
4064 atomic_dec(&cp->reset_task_pending);
4065#else
4066 atomic_set(&cp->reset_task_pending, 0);
4067#endif
4068}
4069
Kees Cook0822c5d2017-10-16 17:29:28 -07004070static void cas_link_timer(struct timer_list *t)
David S. Miller1f26dac2005-09-27 15:24:13 -07004071{
Kees Cook0822c5d2017-10-16 17:29:28 -07004072 struct cas *cp = from_timer(cp, t, link_timer);
David S. Miller1f26dac2005-09-27 15:24:13 -07004073 int mask, pending = 0, reset = 0;
4074 unsigned long flags;
4075
4076 if (link_transition_timeout != 0 &&
4077 cp->link_transition_jiffies_valid &&
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004078 ((jiffies - cp->link_transition_jiffies) >
David S. Miller1f26dac2005-09-27 15:24:13 -07004079 (link_transition_timeout))) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004080 /* One-second counter so link-down workaround doesn't
David S. Miller1f26dac2005-09-27 15:24:13 -07004081 * cause resets to occur so fast as to fool the switch
4082 * into thinking the link is down.
4083 */
4084 cp->link_transition_jiffies_valid = 0;
4085 }
4086
4087 if (!cp->hw_running)
4088 return;
4089
4090 spin_lock_irqsave(&cp->lock, flags);
4091 cas_lock_tx(cp);
4092 cas_entropy_gather(cp);
4093
4094 /* If the link task is still pending, we just
4095 * reschedule the link timer
4096 */
4097#if 1
4098 if (atomic_read(&cp->reset_task_pending_all) ||
4099 atomic_read(&cp->reset_task_pending_spare) ||
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004100 atomic_read(&cp->reset_task_pending_mtu))
David S. Miller1f26dac2005-09-27 15:24:13 -07004101 goto done;
4102#else
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004103 if (atomic_read(&cp->reset_task_pending))
David S. Miller1f26dac2005-09-27 15:24:13 -07004104 goto done;
4105#endif
4106
4107 /* check for rx cleaning */
4108 if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
4109 int i, rmask;
4110
4111 for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
4112 rmask = CAS_FLAG_RXD_POST(i);
4113 if ((mask & rmask) == 0)
4114 continue;
4115
4116 /* post_rxds will do a mod_timer */
4117 if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
4118 pending = 1;
4119 continue;
4120 }
4121 cp->cas_flags &= ~rmask;
4122 }
4123 }
4124
4125 if (CAS_PHY_MII(cp->phy_type)) {
4126 u16 bmsr;
4127 cas_mif_poll(cp, 0);
4128 bmsr = cas_phy_read(cp, MII_BMSR);
4129 /* WTZ: Solaris driver reads this twice, but that
4130 * may be due to the PCS case and the use of a
4131 * common implementation. Read it twice here to be
4132 * safe.
4133 */
4134 bmsr = cas_phy_read(cp, MII_BMSR);
4135 cas_mif_poll(cp, 1);
4136 readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
4137 reset = cas_mii_link_check(cp, bmsr);
4138 } else {
4139 reset = cas_pcs_link_check(cp);
4140 }
4141
4142 if (reset)
4143 goto done;
4144
4145 /* check for tx state machine confusion */
4146 if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
4147 u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
4148 u32 wptr, rptr;
4149 int tlm = CAS_VAL(MAC_SM_TLM, val);
4150
4151 if (((tlm == 0x5) || (tlm == 0x3)) &&
4152 (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
Joe Perches436d27d2010-02-17 15:01:53 +00004153 netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4154 "tx err: MAC_STATE[%08x]\n", val);
David S. Miller1f26dac2005-09-27 15:24:13 -07004155 reset = 1;
4156 goto done;
4157 }
4158
4159 val = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
4160 wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
4161 rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
4162 if ((val == 0) && (wptr != rptr)) {
Joe Perches436d27d2010-02-17 15:01:53 +00004163 netif_printk(cp, tx_err, KERN_DEBUG, cp->dev,
4164 "tx err: TX_FIFO[%08x:%08x:%08x]\n",
4165 val, wptr, rptr);
David S. Miller1f26dac2005-09-27 15:24:13 -07004166 reset = 1;
4167 }
4168
4169 if (reset)
4170 cas_hard_reset(cp);
4171 }
4172
4173done:
4174 if (reset) {
4175#if 1
4176 atomic_inc(&cp->reset_task_pending);
4177 atomic_inc(&cp->reset_task_pending_all);
4178 schedule_work(&cp->reset_task);
4179#else
4180 atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
Joe Perches436d27d2010-02-17 15:01:53 +00004181 pr_err("reset called in cas_link_timer\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004182 schedule_work(&cp->reset_task);
4183#endif
4184 }
4185
4186 if (!pending)
4187 mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
4188 cas_unlock_tx(cp);
4189 spin_unlock_irqrestore(&cp->lock, flags);
4190}
4191
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004192/* tiny buffers are used to avoid target abort issues with
David S. Miller1f26dac2005-09-27 15:24:13 -07004193 * older cassini's
4194 */
4195static void cas_tx_tiny_free(struct cas *cp)
4196{
4197 struct pci_dev *pdev = cp->pdev;
4198 int i;
4199
4200 for (i = 0; i < N_TX_RINGS; i++) {
4201 if (!cp->tx_tiny_bufs[i])
4202 continue;
4203
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004204 pci_free_consistent(pdev, TX_TINY_BUF_BLOCK,
David S. Miller1f26dac2005-09-27 15:24:13 -07004205 cp->tx_tiny_bufs[i],
4206 cp->tx_tiny_dvma[i]);
4207 cp->tx_tiny_bufs[i] = NULL;
4208 }
4209}
4210
4211static int cas_tx_tiny_alloc(struct cas *cp)
4212{
4213 struct pci_dev *pdev = cp->pdev;
4214 int i;
4215
4216 for (i = 0; i < N_TX_RINGS; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004217 cp->tx_tiny_bufs[i] =
David S. Miller1f26dac2005-09-27 15:24:13 -07004218 pci_alloc_consistent(pdev, TX_TINY_BUF_BLOCK,
4219 &cp->tx_tiny_dvma[i]);
4220 if (!cp->tx_tiny_bufs[i]) {
4221 cas_tx_tiny_free(cp);
4222 return -1;
4223 }
4224 }
4225 return 0;
4226}
4227
4228
4229static int cas_open(struct net_device *dev)
4230{
4231 struct cas *cp = netdev_priv(dev);
4232 int hw_was_up, err;
4233 unsigned long flags;
4234
Ingo Molnar758df692006-03-20 22:34:09 -08004235 mutex_lock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004236
4237 hw_was_up = cp->hw_running;
4238
Ingo Molnar758df692006-03-20 22:34:09 -08004239 /* The power-management mutex protects the hw_running
David S. Miller1f26dac2005-09-27 15:24:13 -07004240 * etc. state so it is safe to do this bit without cp->lock
4241 */
4242 if (!cp->hw_running) {
4243 /* Reset the chip */
4244 cas_lock_all_save(cp, flags);
4245 /* We set the second arg to cas_reset to zero
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004246 * because cas_init_hw below will have its second
David S. Miller1f26dac2005-09-27 15:24:13 -07004247 * argument set to non-zero, which will force
4248 * autonegotiation to start.
4249 */
4250 cas_reset(cp, 0);
4251 cp->hw_running = 1;
4252 cas_unlock_all_restore(cp, flags);
4253 }
4254
Jiri Slaby87d75b522009-11-05 23:14:29 +00004255 err = -ENOMEM;
David S. Miller1f26dac2005-09-27 15:24:13 -07004256 if (cas_tx_tiny_alloc(cp) < 0)
Jiri Slaby87d75b522009-11-05 23:14:29 +00004257 goto err_unlock;
David S. Miller1f26dac2005-09-27 15:24:13 -07004258
4259 /* alloc rx descriptors */
David S. Miller1f26dac2005-09-27 15:24:13 -07004260 if (cas_alloc_rxds(cp) < 0)
4261 goto err_tx_tiny;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004262
David S. Miller1f26dac2005-09-27 15:24:13 -07004263 /* allocate spares */
4264 cas_spare_init(cp);
4265 cas_spare_recover(cp, GFP_KERNEL);
4266
4267 /* We can now request the interrupt as we know it's masked
4268 * on the controller. cassini+ has up to 4 interrupts
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004269 * that can be used, but you need to do explicit pci interrupt
David S. Miller1f26dac2005-09-27 15:24:13 -07004270 * mapping to expose them
4271 */
4272 if (request_irq(cp->pdev->irq, cas_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07004273 IRQF_SHARED, dev->name, (void *) dev)) {
Joe Perches436d27d2010-02-17 15:01:53 +00004274 netdev_err(cp->dev, "failed to request irq !\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004275 err = -EAGAIN;
4276 goto err_spare;
4277 }
4278
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004279#ifdef USE_NAPI
4280 napi_enable(&cp->napi);
4281#endif
David S. Miller1f26dac2005-09-27 15:24:13 -07004282 /* init hw */
4283 cas_lock_all_save(cp, flags);
4284 cas_clean_rings(cp);
4285 cas_init_hw(cp, !hw_was_up);
4286 cp->opened = 1;
4287 cas_unlock_all_restore(cp, flags);
4288
4289 netif_start_queue(dev);
Ingo Molnar758df692006-03-20 22:34:09 -08004290 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004291 return 0;
4292
4293err_spare:
4294 cas_spare_free(cp);
4295 cas_free_rxds(cp);
4296err_tx_tiny:
4297 cas_tx_tiny_free(cp);
Jiri Slaby87d75b522009-11-05 23:14:29 +00004298err_unlock:
Ingo Molnar758df692006-03-20 22:34:09 -08004299 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004300 return err;
4301}
4302
4303static int cas_close(struct net_device *dev)
4304{
4305 unsigned long flags;
4306 struct cas *cp = netdev_priv(dev);
4307
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004308#ifdef USE_NAPI
David S. Miller86216262008-01-04 00:23:18 -08004309 napi_disable(&cp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004310#endif
David S. Miller1f26dac2005-09-27 15:24:13 -07004311 /* Make sure we don't get distracted by suspend/resume */
Ingo Molnar758df692006-03-20 22:34:09 -08004312 mutex_lock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004313
4314 netif_stop_queue(dev);
4315
4316 /* Stop traffic, mark us closed */
4317 cas_lock_all_save(cp, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004318 cp->opened = 0;
David S. Miller1f26dac2005-09-27 15:24:13 -07004319 cas_reset(cp, 0);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004320 cas_phy_init(cp);
David S. Miller1f26dac2005-09-27 15:24:13 -07004321 cas_begin_auto_negotiation(cp, NULL);
4322 cas_clean_rings(cp);
4323 cas_unlock_all_restore(cp, flags);
4324
4325 free_irq(cp->pdev->irq, (void *) dev);
4326 cas_spare_free(cp);
4327 cas_free_rxds(cp);
4328 cas_tx_tiny_free(cp);
Ingo Molnar758df692006-03-20 22:34:09 -08004329 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004330 return 0;
4331}
4332
4333static struct {
4334 const char name[ETH_GSTRING_LEN];
4335} ethtool_cassini_statnames[] = {
4336 {"collisions"},
4337 {"rx_bytes"},
4338 {"rx_crc_errors"},
4339 {"rx_dropped"},
4340 {"rx_errors"},
4341 {"rx_fifo_errors"},
4342 {"rx_frame_errors"},
4343 {"rx_length_errors"},
4344 {"rx_over_errors"},
4345 {"rx_packets"},
4346 {"tx_aborted_errors"},
4347 {"tx_bytes"},
4348 {"tx_dropped"},
4349 {"tx_errors"},
4350 {"tx_fifo_errors"},
4351 {"tx_packets"}
4352};
Alejandro Martinez Ruiz4c3616c2007-10-18 10:00:15 +02004353#define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
David S. Miller1f26dac2005-09-27 15:24:13 -07004354
4355static struct {
4356 const int offsets; /* neg. values for 2nd arg to cas_read_phy */
4357} ethtool_register_table[] = {
4358 {-MII_BMSR},
4359 {-MII_BMCR},
4360 {REG_CAWR},
4361 {REG_INF_BURST},
4362 {REG_BIM_CFG},
4363 {REG_RX_CFG},
4364 {REG_HP_CFG},
4365 {REG_MAC_TX_CFG},
4366 {REG_MAC_RX_CFG},
4367 {REG_MAC_CTRL_CFG},
4368 {REG_MAC_XIF_CFG},
4369 {REG_MIF_CFG},
4370 {REG_PCS_CFG},
4371 {REG_SATURN_PCFG},
4372 {REG_PCS_MII_STATUS},
4373 {REG_PCS_STATE_MACHINE},
4374 {REG_MAC_COLL_EXCESS},
4375 {REG_MAC_COLL_LATE}
4376};
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +02004377#define CAS_REG_LEN ARRAY_SIZE(ethtool_register_table)
David S. Miller1f26dac2005-09-27 15:24:13 -07004378#define CAS_MAX_REGS (sizeof (u32)*CAS_REG_LEN)
4379
Al Viroa232f762005-10-03 14:01:37 -07004380static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
David S. Miller1f26dac2005-09-27 15:24:13 -07004381{
David S. Miller1f26dac2005-09-27 15:24:13 -07004382 u8 *p;
4383 int i;
4384 unsigned long flags;
4385
David S. Miller1f26dac2005-09-27 15:24:13 -07004386 spin_lock_irqsave(&cp->lock, flags);
Al Viroa232f762005-10-03 14:01:37 -07004387 for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
David S. Miller1f26dac2005-09-27 15:24:13 -07004388 u16 hval;
4389 u32 val;
4390 if (ethtool_register_table[i].offsets < 0) {
4391 hval = cas_phy_read(cp,
4392 -ethtool_register_table[i].offsets);
4393 val = hval;
4394 } else {
4395 val= readl(cp->regs+ethtool_register_table[i].offsets);
4396 }
4397 memcpy(p, (u8 *)&val, sizeof(u32));
4398 }
4399 spin_unlock_irqrestore(&cp->lock, flags);
David S. Miller1f26dac2005-09-27 15:24:13 -07004400}
4401
4402static struct net_device_stats *cas_get_stats(struct net_device *dev)
4403{
4404 struct cas *cp = netdev_priv(dev);
4405 struct net_device_stats *stats = cp->net_stats;
4406 unsigned long flags;
4407 int i;
4408 unsigned long tmp;
4409
4410 /* we collate all of the stats into net_stats[N_TX_RING] */
4411 if (!cp->hw_running)
4412 return stats + N_TX_RINGS;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004413
David S. Miller1f26dac2005-09-27 15:24:13 -07004414 /* collect outstanding stats */
4415 /* WTZ: the Cassini spec gives these as 16 bit counters but
4416 * stored in 32-bit words. Added a mask of 0xffff to be safe,
4417 * in case the chip somehow puts any garbage in the other bits.
4418 * Also, counter usage didn't seem to mach what Adrian did
4419 * in the parts of the code that set these quantities. Made
4420 * that consistent.
4421 */
4422 spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004423 stats[N_TX_RINGS].rx_crc_errors +=
David S. Miller1f26dac2005-09-27 15:24:13 -07004424 readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004425 stats[N_TX_RINGS].rx_frame_errors +=
David S. Miller1f26dac2005-09-27 15:24:13 -07004426 readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004427 stats[N_TX_RINGS].rx_length_errors +=
David S. Miller1f26dac2005-09-27 15:24:13 -07004428 readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
4429#if 1
4430 tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
4431 (readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
4432 stats[N_TX_RINGS].tx_aborted_errors += tmp;
4433 stats[N_TX_RINGS].collisions +=
4434 tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
4435#else
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004436 stats[N_TX_RINGS].tx_aborted_errors +=
David S. Miller1f26dac2005-09-27 15:24:13 -07004437 readl(cp->regs + REG_MAC_COLL_EXCESS);
4438 stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
4439 readl(cp->regs + REG_MAC_COLL_LATE);
4440#endif
4441 cas_clear_mac_err(cp);
4442
4443 /* saved bits that are unique to ring 0 */
4444 spin_lock(&cp->stat_lock[0]);
4445 stats[N_TX_RINGS].collisions += stats[0].collisions;
4446 stats[N_TX_RINGS].rx_over_errors += stats[0].rx_over_errors;
4447 stats[N_TX_RINGS].rx_frame_errors += stats[0].rx_frame_errors;
4448 stats[N_TX_RINGS].rx_fifo_errors += stats[0].rx_fifo_errors;
4449 stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
4450 stats[N_TX_RINGS].tx_fifo_errors += stats[0].tx_fifo_errors;
4451 spin_unlock(&cp->stat_lock[0]);
4452
4453 for (i = 0; i < N_TX_RINGS; i++) {
4454 spin_lock(&cp->stat_lock[i]);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004455 stats[N_TX_RINGS].rx_length_errors +=
David S. Miller1f26dac2005-09-27 15:24:13 -07004456 stats[i].rx_length_errors;
4457 stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
4458 stats[N_TX_RINGS].rx_packets += stats[i].rx_packets;
4459 stats[N_TX_RINGS].tx_packets += stats[i].tx_packets;
4460 stats[N_TX_RINGS].rx_bytes += stats[i].rx_bytes;
4461 stats[N_TX_RINGS].tx_bytes += stats[i].tx_bytes;
4462 stats[N_TX_RINGS].rx_errors += stats[i].rx_errors;
4463 stats[N_TX_RINGS].tx_errors += stats[i].tx_errors;
4464 stats[N_TX_RINGS].rx_dropped += stats[i].rx_dropped;
4465 stats[N_TX_RINGS].tx_dropped += stats[i].tx_dropped;
4466 memset(stats + i, 0, sizeof(struct net_device_stats));
4467 spin_unlock(&cp->stat_lock[i]);
4468 }
4469 spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
4470 return stats + N_TX_RINGS;
4471}
4472
4473
4474static void cas_set_multicast(struct net_device *dev)
4475{
4476 struct cas *cp = netdev_priv(dev);
4477 u32 rxcfg, rxcfg_new;
4478 unsigned long flags;
4479 int limit = STOP_TRIES;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004480
David S. Miller1f26dac2005-09-27 15:24:13 -07004481 if (!cp->hw_running)
4482 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004483
David S. Miller1f26dac2005-09-27 15:24:13 -07004484 spin_lock_irqsave(&cp->lock, flags);
4485 rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
4486
4487 /* disable RX MAC and wait for completion */
4488 writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4489 while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
4490 if (!limit--)
4491 break;
4492 udelay(10);
4493 }
4494
4495 /* disable hash filter and wait for completion */
4496 limit = STOP_TRIES;
4497 rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
4498 writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
4499 while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
4500 if (!limit--)
4501 break;
4502 udelay(10);
4503 }
4504
4505 /* program hash filters */
4506 cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
4507 rxcfg |= rxcfg_new;
4508 writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
4509 spin_unlock_irqrestore(&cp->lock, flags);
4510}
4511
Al Viroa232f762005-10-03 14:01:37 -07004512static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
4513{
4514 struct cas *cp = netdev_priv(dev);
Rick Jones612a94d2011-11-14 08:13:25 +00004515 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
4516 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
4517 strlcpy(info->bus_info, pci_name(cp->pdev), sizeof(info->bus_info));
Al Viroa232f762005-10-03 14:01:37 -07004518}
4519
Philippe Reynes2c784b02017-03-04 16:16:12 +01004520static int cas_get_link_ksettings(struct net_device *dev,
4521 struct ethtool_link_ksettings *cmd)
David S. Miller1f26dac2005-09-27 15:24:13 -07004522{
4523 struct cas *cp = netdev_priv(dev);
4524 u16 bmcr;
4525 int full_duplex, speed, pause;
David S. Miller1f26dac2005-09-27 15:24:13 -07004526 unsigned long flags;
4527 enum link_state linkstate = link_up;
Philippe Reynes2c784b02017-03-04 16:16:12 +01004528 u32 supported, advertising;
David S. Miller1f26dac2005-09-27 15:24:13 -07004529
Philippe Reynes2c784b02017-03-04 16:16:12 +01004530 advertising = 0;
4531 supported = SUPPORTED_Autoneg;
Al Viroa232f762005-10-03 14:01:37 -07004532 if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004533 supported |= SUPPORTED_1000baseT_Full;
4534 advertising |= ADVERTISED_1000baseT_Full;
David S. Miller1f26dac2005-09-27 15:24:13 -07004535 }
4536
Al Viroa232f762005-10-03 14:01:37 -07004537 /* Record PHY settings if HW is on. */
4538 spin_lock_irqsave(&cp->lock, flags);
4539 bmcr = 0;
4540 linkstate = cp->lstate;
4541 if (CAS_PHY_MII(cp->phy_type)) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004542 cmd->base.port = PORT_MII;
4543 cmd->base.phy_address = cp->phy_addr;
4544 advertising |= ADVERTISED_TP | ADVERTISED_MII |
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004545 ADVERTISED_10baseT_Half |
4546 ADVERTISED_10baseT_Full |
4547 ADVERTISED_100baseT_Half |
Al Viroa232f762005-10-03 14:01:37 -07004548 ADVERTISED_100baseT_Full;
4549
Philippe Reynes2c784b02017-03-04 16:16:12 +01004550 supported |=
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004551 (SUPPORTED_10baseT_Half |
Al Viroa232f762005-10-03 14:01:37 -07004552 SUPPORTED_10baseT_Full |
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004553 SUPPORTED_100baseT_Half |
Al Viroa232f762005-10-03 14:01:37 -07004554 SUPPORTED_100baseT_Full |
4555 SUPPORTED_TP | SUPPORTED_MII);
4556
4557 if (cp->hw_running) {
4558 cas_mif_poll(cp, 0);
4559 bmcr = cas_phy_read(cp, MII_BMCR);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004560 cas_read_mii_link_mode(cp, &full_duplex,
Al Viroa232f762005-10-03 14:01:37 -07004561 &speed, &pause);
4562 cas_mif_poll(cp, 1);
David S. Miller1f26dac2005-09-27 15:24:13 -07004563 }
4564
Al Viroa232f762005-10-03 14:01:37 -07004565 } else {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004566 cmd->base.port = PORT_FIBRE;
4567 cmd->base.phy_address = 0;
4568 supported |= SUPPORTED_FIBRE;
4569 advertising |= ADVERTISED_FIBRE;
David S. Miller1f26dac2005-09-27 15:24:13 -07004570
Al Viroa232f762005-10-03 14:01:37 -07004571 if (cp->hw_running) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004572 /* pcs uses the same bits as mii */
Al Viroa232f762005-10-03 14:01:37 -07004573 bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004574 cas_read_pcs_link_mode(cp, &full_duplex,
Al Viroa232f762005-10-03 14:01:37 -07004575 &speed, &pause);
David S. Miller1f26dac2005-09-27 15:24:13 -07004576 }
Al Viroa232f762005-10-03 14:01:37 -07004577 }
4578 spin_unlock_irqrestore(&cp->lock, flags);
David S. Miller1f26dac2005-09-27 15:24:13 -07004579
Al Viroa232f762005-10-03 14:01:37 -07004580 if (bmcr & BMCR_ANENABLE) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004581 advertising |= ADVERTISED_Autoneg;
4582 cmd->base.autoneg = AUTONEG_ENABLE;
4583 cmd->base.speed = ((speed == 10) ?
David Decotigny70739492011-04-27 18:32:40 +00004584 SPEED_10 :
4585 ((speed == 1000) ?
Philippe Reynes2c784b02017-03-04 16:16:12 +01004586 SPEED_1000 : SPEED_100));
4587 cmd->base.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
Al Viroa232f762005-10-03 14:01:37 -07004588 } else {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004589 cmd->base.autoneg = AUTONEG_DISABLE;
4590 cmd->base.speed = ((bmcr & CAS_BMCR_SPEED1000) ?
David Decotigny70739492011-04-27 18:32:40 +00004591 SPEED_1000 :
4592 ((bmcr & BMCR_SPEED100) ?
Philippe Reynes2c784b02017-03-04 16:16:12 +01004593 SPEED_100 : SPEED_10));
4594 cmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
Al Viroa232f762005-10-03 14:01:37 -07004595 DUPLEX_FULL : DUPLEX_HALF;
4596 }
4597 if (linkstate != link_up) {
4598 /* Force these to "unknown" if the link is not up and
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004599 * autonogotiation in enabled. We can set the link
Al Viroa232f762005-10-03 14:01:37 -07004600 * speed to 0, but not cmd->duplex,
4601 * because its legal values are 0 and 1. Ethtool will
4602 * print the value reported in parentheses after the
4603 * word "Unknown" for unrecognized values.
4604 *
4605 * If in forced mode, we report the speed and duplex
4606 * settings that we configured.
4607 */
4608 if (cp->link_cntl & BMCR_ANENABLE) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004609 cmd->base.speed = 0;
4610 cmd->base.duplex = 0xff;
David S. Miller1f26dac2005-09-27 15:24:13 -07004611 } else {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004612 cmd->base.speed = SPEED_10;
Al Viroa232f762005-10-03 14:01:37 -07004613 if (cp->link_cntl & BMCR_SPEED100) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004614 cmd->base.speed = SPEED_100;
Al Viroa232f762005-10-03 14:01:37 -07004615 } else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
Philippe Reynes2c784b02017-03-04 16:16:12 +01004616 cmd->base.speed = SPEED_1000;
Al Viroa232f762005-10-03 14:01:37 -07004617 }
Philippe Reynes2c784b02017-03-04 16:16:12 +01004618 cmd->base.duplex = (cp->link_cntl & BMCR_FULLDPLX) ?
David S. Miller1f26dac2005-09-27 15:24:13 -07004619 DUPLEX_FULL : DUPLEX_HALF;
4620 }
David S. Miller1f26dac2005-09-27 15:24:13 -07004621 }
Philippe Reynes2c784b02017-03-04 16:16:12 +01004622
4623 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
4624 supported);
4625 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
4626 advertising);
4627
Al Viroa232f762005-10-03 14:01:37 -07004628 return 0;
David S. Miller1f26dac2005-09-27 15:24:13 -07004629}
4630
Philippe Reynes2c784b02017-03-04 16:16:12 +01004631static int cas_set_link_ksettings(struct net_device *dev,
4632 const struct ethtool_link_ksettings *cmd)
Al Viroa232f762005-10-03 14:01:37 -07004633{
4634 struct cas *cp = netdev_priv(dev);
4635 unsigned long flags;
Philippe Reynes2c784b02017-03-04 16:16:12 +01004636 u32 speed = cmd->base.speed;
Al Viroa232f762005-10-03 14:01:37 -07004637
4638 /* Verify the settings we care about. */
Philippe Reynes2c784b02017-03-04 16:16:12 +01004639 if (cmd->base.autoneg != AUTONEG_ENABLE &&
4640 cmd->base.autoneg != AUTONEG_DISABLE)
Al Viroa232f762005-10-03 14:01:37 -07004641 return -EINVAL;
4642
Philippe Reynes2c784b02017-03-04 16:16:12 +01004643 if (cmd->base.autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +00004644 ((speed != SPEED_1000 &&
4645 speed != SPEED_100 &&
4646 speed != SPEED_10) ||
Philippe Reynes2c784b02017-03-04 16:16:12 +01004647 (cmd->base.duplex != DUPLEX_HALF &&
4648 cmd->base.duplex != DUPLEX_FULL)))
Al Viroa232f762005-10-03 14:01:37 -07004649 return -EINVAL;
4650
4651 /* Apply settings and restart link process. */
4652 spin_lock_irqsave(&cp->lock, flags);
4653 cas_begin_auto_negotiation(cp, cmd);
4654 spin_unlock_irqrestore(&cp->lock, flags);
4655 return 0;
4656}
4657
4658static int cas_nway_reset(struct net_device *dev)
4659{
4660 struct cas *cp = netdev_priv(dev);
4661 unsigned long flags;
4662
4663 if ((cp->link_cntl & BMCR_ANENABLE) == 0)
4664 return -EINVAL;
4665
4666 /* Restart link process. */
4667 spin_lock_irqsave(&cp->lock, flags);
4668 cas_begin_auto_negotiation(cp, NULL);
4669 spin_unlock_irqrestore(&cp->lock, flags);
4670
4671 return 0;
4672}
4673
4674static u32 cas_get_link(struct net_device *dev)
4675{
4676 struct cas *cp = netdev_priv(dev);
4677 return cp->lstate == link_up;
4678}
4679
4680static u32 cas_get_msglevel(struct net_device *dev)
4681{
4682 struct cas *cp = netdev_priv(dev);
4683 return cp->msg_enable;
4684}
4685
4686static void cas_set_msglevel(struct net_device *dev, u32 value)
4687{
4688 struct cas *cp = netdev_priv(dev);
4689 cp->msg_enable = value;
4690}
4691
4692static int cas_get_regs_len(struct net_device *dev)
4693{
4694 struct cas *cp = netdev_priv(dev);
4695 return cp->casreg_len < CAS_MAX_REGS ? cp->casreg_len: CAS_MAX_REGS;
4696}
4697
4698static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
4699 void *p)
4700{
4701 struct cas *cp = netdev_priv(dev);
4702 regs->version = 0;
4703 /* cas_read_regs handles locks (cp->lock). */
4704 cas_read_regs(cp, p, regs->len / sizeof(u32));
4705}
4706
Jeff Garzikb9f2c042007-10-03 18:07:32 -07004707static int cas_get_sset_count(struct net_device *dev, int sset)
Al Viroa232f762005-10-03 14:01:37 -07004708{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07004709 switch (sset) {
4710 case ETH_SS_STATS:
4711 return CAS_NUM_STAT_KEYS;
4712 default:
4713 return -EOPNOTSUPP;
4714 }
Al Viroa232f762005-10-03 14:01:37 -07004715}
4716
4717static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4718{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004719 memcpy(data, &ethtool_cassini_statnames,
Al Viroa232f762005-10-03 14:01:37 -07004720 CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
4721}
4722
4723static void cas_get_ethtool_stats(struct net_device *dev,
4724 struct ethtool_stats *estats, u64 *data)
4725{
4726 struct cas *cp = netdev_priv(dev);
4727 struct net_device_stats *stats = cas_get_stats(cp->dev);
4728 int i = 0;
4729 data[i++] = stats->collisions;
4730 data[i++] = stats->rx_bytes;
4731 data[i++] = stats->rx_crc_errors;
4732 data[i++] = stats->rx_dropped;
4733 data[i++] = stats->rx_errors;
4734 data[i++] = stats->rx_fifo_errors;
4735 data[i++] = stats->rx_frame_errors;
4736 data[i++] = stats->rx_length_errors;
4737 data[i++] = stats->rx_over_errors;
4738 data[i++] = stats->rx_packets;
4739 data[i++] = stats->tx_aborted_errors;
4740 data[i++] = stats->tx_bytes;
4741 data[i++] = stats->tx_dropped;
4742 data[i++] = stats->tx_errors;
4743 data[i++] = stats->tx_fifo_errors;
4744 data[i++] = stats->tx_packets;
4745 BUG_ON(i != CAS_NUM_STAT_KEYS);
4746}
4747
Jeff Garzik7282d492006-09-13 14:30:00 -04004748static const struct ethtool_ops cas_ethtool_ops = {
Al Viroa232f762005-10-03 14:01:37 -07004749 .get_drvinfo = cas_get_drvinfo,
Al Viroa232f762005-10-03 14:01:37 -07004750 .nway_reset = cas_nway_reset,
4751 .get_link = cas_get_link,
4752 .get_msglevel = cas_get_msglevel,
4753 .set_msglevel = cas_set_msglevel,
4754 .get_regs_len = cas_get_regs_len,
4755 .get_regs = cas_get_regs,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07004756 .get_sset_count = cas_get_sset_count,
Al Viroa232f762005-10-03 14:01:37 -07004757 .get_strings = cas_get_strings,
4758 .get_ethtool_stats = cas_get_ethtool_stats,
Philippe Reynes2c784b02017-03-04 16:16:12 +01004759 .get_link_ksettings = cas_get_link_ksettings,
4760 .set_link_ksettings = cas_set_link_ksettings,
Al Viroa232f762005-10-03 14:01:37 -07004761};
4762
David S. Miller1f26dac2005-09-27 15:24:13 -07004763static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4764{
4765 struct cas *cp = netdev_priv(dev);
Al Viro46d70312005-09-30 03:21:45 +01004766 struct mii_ioctl_data *data = if_mii(ifr);
David S. Miller1f26dac2005-09-27 15:24:13 -07004767 unsigned long flags;
4768 int rc = -EOPNOTSUPP;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004769
Ingo Molnar758df692006-03-20 22:34:09 -08004770 /* Hold the PM mutex while doing ioctl's or we may collide
David S. Miller1f26dac2005-09-27 15:24:13 -07004771 * with open/close and power management and oops.
4772 */
Ingo Molnar758df692006-03-20 22:34:09 -08004773 mutex_lock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004774 switch (cmd) {
David S. Miller1f26dac2005-09-27 15:24:13 -07004775 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
4776 data->phy_id = cp->phy_addr;
4777 /* Fallthrough... */
4778
4779 case SIOCGMIIREG: /* Read MII PHY register. */
4780 spin_lock_irqsave(&cp->lock, flags);
4781 cas_mif_poll(cp, 0);
4782 data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
4783 cas_mif_poll(cp, 1);
4784 spin_unlock_irqrestore(&cp->lock, flags);
4785 rc = 0;
4786 break;
4787
4788 case SIOCSMIIREG: /* Write MII PHY register. */
David S. Miller1f26dac2005-09-27 15:24:13 -07004789 spin_lock_irqsave(&cp->lock, flags);
4790 cas_mif_poll(cp, 0);
4791 rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
4792 cas_mif_poll(cp, 1);
4793 spin_unlock_irqrestore(&cp->lock, flags);
4794 break;
4795 default:
4796 break;
Joe Perchesee289b62010-05-17 22:47:34 -07004797 }
David S. Miller1f26dac2005-09-27 15:24:13 -07004798
Ingo Molnar758df692006-03-20 22:34:09 -08004799 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07004800 return rc;
4801}
4802
David S. Miller9e1848b2008-01-03 20:11:31 -08004803/* When this chip sits underneath an Intel 31154 bridge, it is the
4804 * only subordinate device and we can tweak the bridge settings to
4805 * reflect that fact.
4806 */
Bill Pembertonf73d12b2012-12-03 09:24:02 -05004807static void cas_program_bridge(struct pci_dev *cas_pdev)
David S. Miller9e1848b2008-01-03 20:11:31 -08004808{
4809 struct pci_dev *pdev = cas_pdev->bus->self;
4810 u32 val;
4811
4812 if (!pdev)
4813 return;
4814
4815 if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
4816 return;
4817
4818 /* Clear bit 10 (Bus Parking Control) in the Secondary
4819 * Arbiter Control/Status Register which lives at offset
4820 * 0x41. Using a 32-bit word read/modify/write at 0x40
4821 * is much simpler so that's how we do this.
4822 */
4823 pci_read_config_dword(pdev, 0x40, &val);
4824 val &= ~0x00040000;
4825 pci_write_config_dword(pdev, 0x40, val);
4826
4827 /* Max out the Multi-Transaction Timer settings since
4828 * Cassini is the only device present.
4829 *
4830 * The register is 16-bit and lives at 0x50. When the
4831 * settings are enabled, it extends the GRANT# signal
4832 * for a requestor after a transaction is complete. This
4833 * allows the next request to run without first needing
4834 * to negotiate the GRANT# signal back.
4835 *
4836 * Bits 12:10 define the grant duration:
4837 *
4838 * 1 -- 16 clocks
4839 * 2 -- 32 clocks
4840 * 3 -- 64 clocks
4841 * 4 -- 128 clocks
4842 * 5 -- 256 clocks
4843 *
4844 * All other values are illegal.
4845 *
4846 * Bits 09:00 define which REQ/GNT signal pairs get the
4847 * GRANT# signal treatment. We set them all.
4848 */
4849 pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
4850
4851 /* The Read Prefecth Policy register is 16-bit and sits at
4852 * offset 0x52. It enables a "smart" pre-fetch policy. We
4853 * enable it and max out all of the settings since only one
4854 * device is sitting underneath and thus bandwidth sharing is
4855 * not an issue.
4856 *
4857 * The register has several 3 bit fields, which indicates a
4858 * multiplier applied to the base amount of prefetching the
4859 * chip would do. These fields are at:
4860 *
4861 * 15:13 --- ReRead Primary Bus
4862 * 12:10 --- FirstRead Primary Bus
4863 * 09:07 --- ReRead Secondary Bus
4864 * 06:04 --- FirstRead Secondary Bus
4865 *
4866 * Bits 03:00 control which REQ/GNT pairs the prefetch settings
4867 * get enabled on. Bit 3 is a grouped enabler which controls
4868 * all of the REQ/GNT pairs from [8:3]. Bits 2 to 0 control
4869 * the individual REQ/GNT pairs [2:0].
4870 */
4871 pci_write_config_word(pdev, 0x52,
4872 (0x7 << 13) |
4873 (0x7 << 10) |
4874 (0x7 << 7) |
4875 (0x7 << 4) |
4876 (0xf << 0));
4877
4878 /* Force cacheline size to 0x8 */
4879 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4880
4881 /* Force latency timer to maximum setting so Cassini can
4882 * sit on the bus as long as it likes.
4883 */
4884 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
4885}
4886
Stephen Hemminger83d6f032009-01-07 17:25:41 -08004887static const struct net_device_ops cas_netdev_ops = {
4888 .ndo_open = cas_open,
4889 .ndo_stop = cas_close,
4890 .ndo_start_xmit = cas_start_xmit,
4891 .ndo_get_stats = cas_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00004892 .ndo_set_rx_mode = cas_set_multicast,
Stephen Hemminger83d6f032009-01-07 17:25:41 -08004893 .ndo_do_ioctl = cas_ioctl,
4894 .ndo_tx_timeout = cas_tx_timeout,
4895 .ndo_change_mtu = cas_change_mtu,
4896 .ndo_set_mac_address = eth_mac_addr,
4897 .ndo_validate_addr = eth_validate_addr,
4898#ifdef CONFIG_NET_POLL_CONTROLLER
4899 .ndo_poll_controller = cas_netpoll,
4900#endif
4901};
4902
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004903static int cas_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
David S. Miller1f26dac2005-09-27 15:24:13 -07004904{
4905 static int cas_version_printed = 0;
Marc Zyngier18e37f22006-04-13 11:38:20 +02004906 unsigned long casreg_len;
David S. Miller1f26dac2005-09-27 15:24:13 -07004907 struct net_device *dev;
4908 struct cas *cp;
4909 int i, err, pci_using_dac;
4910 u16 pci_cmd;
4911 u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
4912
4913 if (cas_version_printed++ == 0)
Joe Perches436d27d2010-02-17 15:01:53 +00004914 pr_info("%s", version);
David S. Miller1f26dac2005-09-27 15:24:13 -07004915
4916 err = pci_enable_device(pdev);
4917 if (err) {
Joe Perches436d27d2010-02-17 15:01:53 +00004918 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004919 return err;
4920 }
4921
4922 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04004923 dev_err(&pdev->dev, "Cannot find proper PCI device "
Joe Perches436d27d2010-02-17 15:01:53 +00004924 "base address, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004925 err = -ENODEV;
4926 goto err_out_disable_pdev;
4927 }
4928
4929 dev = alloc_etherdev(sizeof(*cp));
4930 if (!dev) {
David S. Miller1f26dac2005-09-27 15:24:13 -07004931 err = -ENOMEM;
4932 goto err_out_disable_pdev;
4933 }
David S. Miller1f26dac2005-09-27 15:24:13 -07004934 SET_NETDEV_DEV(dev, &pdev->dev);
4935
4936 err = pci_request_regions(pdev, dev->name);
4937 if (err) {
Joe Perches436d27d2010-02-17 15:01:53 +00004938 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004939 goto err_out_free_netdev;
4940 }
4941 pci_set_master(pdev);
4942
4943 /* we must always turn on parity response or else parity
4944 * doesn't get generated properly. disable SERR/PERR as well.
4945 * in addition, we want to turn MWI on.
4946 */
4947 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4948 pci_cmd &= ~PCI_COMMAND_SERR;
4949 pci_cmd |= PCI_COMMAND_PARITY;
4950 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Randy Dunlap694625c2007-07-09 11:55:54 -07004951 if (pci_try_set_mwi(pdev))
Joe Perchesfe3881c2014-09-09 20:27:44 -07004952 pr_warn("Could not enable MWI for %s\n", pci_name(pdev));
David S. Miller04efb872007-05-24 17:54:15 -07004953
David S. Miller9e1848b2008-01-03 20:11:31 -08004954 cas_program_bridge(pdev);
4955
David S. Miller1f26dac2005-09-27 15:24:13 -07004956 /*
4957 * On some architectures, the default cache line size set
Randy Dunlap694625c2007-07-09 11:55:54 -07004958 * by pci_try_set_mwi reduces perforamnce. We have to increase
David S. Miller1f26dac2005-09-27 15:24:13 -07004959 * it for this case. To start, we'll print some configuration
4960 * data.
4961 */
4962#if 1
4963 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
4964 &orig_cacheline_size);
4965 if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004966 cas_cacheline_size =
4967 (CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
David S. Miller1f26dac2005-09-27 15:24:13 -07004968 CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004969 if (pci_write_config_byte(pdev,
4970 PCI_CACHE_LINE_SIZE,
David S. Miller1f26dac2005-09-27 15:24:13 -07004971 cas_cacheline_size)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04004972 dev_err(&pdev->dev, "Could not set PCI cache "
David S. Miller1f26dac2005-09-27 15:24:13 -07004973 "line size\n");
4974 goto err_write_cacheline;
4975 }
4976 }
4977#endif
4978
4979
4980 /* Configure DMA attributes. */
Yang Hongyang6a355282009-04-06 19:01:13 -07004981 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
David S. Miller1f26dac2005-09-27 15:24:13 -07004982 pci_using_dac = 1;
4983 err = pci_set_consistent_dma_mask(pdev,
Yang Hongyang6a355282009-04-06 19:01:13 -07004984 DMA_BIT_MASK(64));
David S. Miller1f26dac2005-09-27 15:24:13 -07004985 if (err < 0) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04004986 dev_err(&pdev->dev, "Unable to obtain 64-bit DMA "
David S. Miller1f26dac2005-09-27 15:24:13 -07004987 "for consistent allocations\n");
4988 goto err_out_free_res;
4989 }
4990
4991 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004992 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
David S. Miller1f26dac2005-09-27 15:24:13 -07004993 if (err) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04004994 dev_err(&pdev->dev, "No usable DMA configuration, "
Joe Perches436d27d2010-02-17 15:01:53 +00004995 "aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07004996 goto err_out_free_res;
4997 }
4998 pci_using_dac = 0;
4999 }
5000
David S. Miller1f26dac2005-09-27 15:24:13 -07005001 casreg_len = pci_resource_len(pdev, 0);
5002
5003 cp = netdev_priv(dev);
5004 cp->pdev = pdev;
5005#if 1
5006 /* A value of 0 indicates we never explicitly set it */
5007 cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
5008#endif
5009 cp->dev = dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005010 cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
David S. Miller1f26dac2005-09-27 15:24:13 -07005011 cassini_debug;
5012
David S. Miller92d76e82011-01-14 12:39:59 -08005013#if defined(CONFIG_SPARC)
Richard Mortimer4e3dbdb2011-01-06 11:50:30 -08005014 cp->of_node = pci_device_to_OF_node(pdev);
5015#endif
5016
David S. Miller1f26dac2005-09-27 15:24:13 -07005017 cp->link_transition = LINK_TRANSITION_UNKNOWN;
5018 cp->link_transition_jiffies_valid = 0;
5019
5020 spin_lock_init(&cp->lock);
5021 spin_lock_init(&cp->rx_inuse_lock);
5022 spin_lock_init(&cp->rx_spare_lock);
5023 for (i = 0; i < N_TX_RINGS; i++) {
5024 spin_lock_init(&cp->stat_lock[i]);
5025 spin_lock_init(&cp->tx_lock[i]);
5026 }
5027 spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
Ingo Molnar758df692006-03-20 22:34:09 -08005028 mutex_init(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005029
Kees Cook0822c5d2017-10-16 17:29:28 -07005030 timer_setup(&cp->link_timer, cas_link_timer, 0);
5031
David S. Miller1f26dac2005-09-27 15:24:13 -07005032#if 1
5033 /* Just in case the implementation of atomic operations
5034 * change so that an explicit initialization is necessary.
5035 */
5036 atomic_set(&cp->reset_task_pending, 0);
5037 atomic_set(&cp->reset_task_pending_all, 0);
5038 atomic_set(&cp->reset_task_pending_spare, 0);
5039 atomic_set(&cp->reset_task_pending_mtu, 0);
5040#endif
David Howellsc4028952006-11-22 14:57:56 +00005041 INIT_WORK(&cp->reset_task, cas_reset_task);
David S. Miller1f26dac2005-09-27 15:24:13 -07005042
5043 /* Default link parameters */
Dan Carpenterbf829372010-03-02 22:22:41 +00005044 if (link_mode >= 0 && link_mode < 6)
David S. Miller1f26dac2005-09-27 15:24:13 -07005045 cp->link_cntl = link_modes[link_mode];
5046 else
5047 cp->link_cntl = BMCR_ANENABLE;
5048 cp->lstate = link_down;
5049 cp->link_transition = LINK_TRANSITION_LINK_DOWN;
5050 netif_carrier_off(cp->dev);
5051 cp->timer_ticks = 0;
5052
5053 /* give us access to cassini registers */
Marc Zyngier18e37f22006-04-13 11:38:20 +02005054 cp->regs = pci_iomap(pdev, 0, casreg_len);
Al Viro79ea13c2008-01-24 02:06:46 -08005055 if (!cp->regs) {
Joe Perches436d27d2010-02-17 15:01:53 +00005056 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07005057 goto err_out_free_res;
5058 }
5059 cp->casreg_len = casreg_len;
5060
5061 pci_save_state(pdev);
5062 cas_check_pci_invariants(cp);
5063 cas_hard_reset(cp);
5064 cas_reset(cp, 0);
5065 if (cas_check_invariants(cp))
5066 goto err_out_iounmap;
Jaswinder Singhfcaa4062008-09-22 19:27:10 -07005067 if (cp->cas_flags & CAS_FLAG_SATURN)
Ben Hutchings15627e82013-07-01 00:13:27 +01005068 cas_saturn_firmware_init(cp);
David S. Miller1f26dac2005-09-27 15:24:13 -07005069
5070 cp->init_block = (struct cas_init_block *)
5071 pci_alloc_consistent(pdev, sizeof(struct cas_init_block),
5072 &cp->block_dvma);
5073 if (!cp->init_block) {
Joe Perches436d27d2010-02-17 15:01:53 +00005074 dev_err(&pdev->dev, "Cannot allocate init block, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07005075 goto err_out_iounmap;
5076 }
5077
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005078 for (i = 0; i < N_TX_RINGS; i++)
David S. Miller1f26dac2005-09-27 15:24:13 -07005079 cp->init_txds[i] = cp->init_block->txds[i];
5080
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005081 for (i = 0; i < N_RX_DESC_RINGS; i++)
David S. Miller1f26dac2005-09-27 15:24:13 -07005082 cp->init_rxds[i] = cp->init_block->rxds[i];
5083
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005084 for (i = 0; i < N_RX_COMP_RINGS; i++)
David S. Miller1f26dac2005-09-27 15:24:13 -07005085 cp->init_rxcs[i] = cp->init_block->rxcs[i];
5086
5087 for (i = 0; i < N_RX_FLOWS; i++)
5088 skb_queue_head_init(&cp->rx_flows[i]);
5089
Stephen Hemminger83d6f032009-01-07 17:25:41 -08005090 dev->netdev_ops = &cas_netdev_ops;
Al Viroa232f762005-10-03 14:01:37 -07005091 dev->ethtool_ops = &cas_ethtool_ops;
David S. Miller1f26dac2005-09-27 15:24:13 -07005092 dev->watchdog_timeo = CAS_TX_TIMEOUT;
Stephen Hemminger83d6f032009-01-07 17:25:41 -08005093
David S. Miller1f26dac2005-09-27 15:24:13 -07005094#ifdef USE_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005095 netif_napi_add(dev, &cp->napi, cas_poll, 64);
David S. Miller1f26dac2005-09-27 15:24:13 -07005096#endif
David S. Miller1f26dac2005-09-27 15:24:13 -07005097 dev->irq = pdev->irq;
5098 dev->dma = 0;
5099
5100 /* Cassini features. */
5101 if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
5102 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
5103
5104 if (pci_using_dac)
5105 dev->features |= NETIF_F_HIGHDMA;
5106
Jarod Wilson540bfe32016-10-17 15:54:10 -04005107 /* MTU range: 60 - varies or 9000 */
5108 dev->min_mtu = CAS_MIN_MTU;
5109 dev->max_mtu = CAS_MAX_MTU;
5110
David S. Miller1f26dac2005-09-27 15:24:13 -07005111 if (register_netdev(dev)) {
Joe Perches436d27d2010-02-17 15:01:53 +00005112 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07005113 goto err_out_free_consistent;
5114 }
5115
5116 i = readl(cp->regs + REG_BIM_CFG);
Joe Perches436d27d2010-02-17 15:01:53 +00005117 netdev_info(dev, "Sun Cassini%s (%sbit/%sMHz PCI/%s) Ethernet[%d] %pM\n",
5118 (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
5119 (i & BIM_CFG_32BIT) ? "32" : "64",
5120 (i & BIM_CFG_66MHZ) ? "66" : "33",
5121 (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
5122 dev->dev_addr);
David S. Miller1f26dac2005-09-27 15:24:13 -07005123
5124 pci_set_drvdata(pdev, dev);
5125 cp->hw_running = 1;
5126 cas_entropy_reset(cp);
5127 cas_phy_init(cp);
5128 cas_begin_auto_negotiation(cp, NULL);
5129 return 0;
5130
5131err_out_free_consistent:
5132 pci_free_consistent(pdev, sizeof(struct cas_init_block),
5133 cp->init_block, cp->block_dvma);
5134
5135err_out_iounmap:
Ingo Molnar758df692006-03-20 22:34:09 -08005136 mutex_lock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005137 if (cp->hw_running)
5138 cas_shutdown(cp);
Ingo Molnar758df692006-03-20 22:34:09 -08005139 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005140
Marc Zyngier18e37f22006-04-13 11:38:20 +02005141 pci_iounmap(pdev, cp->regs);
David S. Miller1f26dac2005-09-27 15:24:13 -07005142
5143
5144err_out_free_res:
5145 pci_release_regions(pdev);
5146
5147err_write_cacheline:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005148 /* Try to restore it in case the error occurred after we
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005149 * set it.
David S. Miller1f26dac2005-09-27 15:24:13 -07005150 */
5151 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
5152
5153err_out_free_netdev:
5154 free_netdev(dev);
5155
5156err_out_disable_pdev:
5157 pci_disable_device(pdev);
David S. Miller1f26dac2005-09-27 15:24:13 -07005158 return -ENODEV;
5159}
5160
Bill Pembertonf73d12b2012-12-03 09:24:02 -05005161static void cas_remove_one(struct pci_dev *pdev)
David S. Miller1f26dac2005-09-27 15:24:13 -07005162{
5163 struct net_device *dev = pci_get_drvdata(pdev);
5164 struct cas *cp;
5165 if (!dev)
5166 return;
5167
5168 cp = netdev_priv(dev);
5169 unregister_netdev(dev);
5170
Markus Elfring39af4552014-11-29 14:34:59 +01005171 vfree(cp->fw_data);
Jaswinder Singhfcaa4062008-09-22 19:27:10 -07005172
Ingo Molnar758df692006-03-20 22:34:09 -08005173 mutex_lock(&cp->pm_mutex);
Tejun Heo23f333a2010-12-12 16:45:14 +01005174 cancel_work_sync(&cp->reset_task);
David S. Miller1f26dac2005-09-27 15:24:13 -07005175 if (cp->hw_running)
5176 cas_shutdown(cp);
Ingo Molnar758df692006-03-20 22:34:09 -08005177 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005178
5179#if 1
5180 if (cp->orig_cacheline_size) {
5181 /* Restore the cache line size if we had modified
5182 * it.
5183 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005184 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
David S. Miller1f26dac2005-09-27 15:24:13 -07005185 cp->orig_cacheline_size);
5186 }
5187#endif
5188 pci_free_consistent(pdev, sizeof(struct cas_init_block),
5189 cp->init_block, cp->block_dvma);
Marc Zyngier18e37f22006-04-13 11:38:20 +02005190 pci_iounmap(pdev, cp->regs);
David S. Miller1f26dac2005-09-27 15:24:13 -07005191 free_netdev(dev);
5192 pci_release_regions(pdev);
5193 pci_disable_device(pdev);
David S. Miller1f26dac2005-09-27 15:24:13 -07005194}
5195
5196#ifdef CONFIG_PM
Al Viro46d70312005-09-30 03:21:45 +01005197static int cas_suspend(struct pci_dev *pdev, pm_message_t state)
David S. Miller1f26dac2005-09-27 15:24:13 -07005198{
5199 struct net_device *dev = pci_get_drvdata(pdev);
5200 struct cas *cp = netdev_priv(dev);
5201 unsigned long flags;
5202
Ingo Molnar758df692006-03-20 22:34:09 -08005203 mutex_lock(&cp->pm_mutex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005204
David S. Miller1f26dac2005-09-27 15:24:13 -07005205 /* If the driver is opened, we stop the DMA */
5206 if (cp->opened) {
5207 netif_device_detach(dev);
5208
5209 cas_lock_all_save(cp, flags);
5210
5211 /* We can set the second arg of cas_reset to 0
5212 * because on resume, we'll call cas_init_hw with
5213 * its second arg set so that autonegotiation is
5214 * restarted.
5215 */
5216 cas_reset(cp, 0);
5217 cas_clean_rings(cp);
5218 cas_unlock_all_restore(cp, flags);
5219 }
5220
5221 if (cp->hw_running)
5222 cas_shutdown(cp);
Ingo Molnar758df692006-03-20 22:34:09 -08005223 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005224
5225 return 0;
5226}
5227
5228static int cas_resume(struct pci_dev *pdev)
5229{
5230 struct net_device *dev = pci_get_drvdata(pdev);
5231 struct cas *cp = netdev_priv(dev);
5232
Joe Perches436d27d2010-02-17 15:01:53 +00005233 netdev_info(dev, "resuming\n");
David S. Miller1f26dac2005-09-27 15:24:13 -07005234
Ingo Molnar758df692006-03-20 22:34:09 -08005235 mutex_lock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005236 cas_hard_reset(cp);
5237 if (cp->opened) {
5238 unsigned long flags;
5239 cas_lock_all_save(cp, flags);
5240 cas_reset(cp, 0);
5241 cp->hw_running = 1;
5242 cas_clean_rings(cp);
5243 cas_init_hw(cp, 1);
5244 cas_unlock_all_restore(cp, flags);
5245
5246 netif_device_attach(dev);
5247 }
Ingo Molnar758df692006-03-20 22:34:09 -08005248 mutex_unlock(&cp->pm_mutex);
David S. Miller1f26dac2005-09-27 15:24:13 -07005249 return 0;
5250}
5251#endif /* CONFIG_PM */
5252
5253static struct pci_driver cas_driver = {
5254 .name = DRV_MODULE_NAME,
5255 .id_table = cas_pci_tbl,
5256 .probe = cas_init_one,
Bill Pembertonf73d12b2012-12-03 09:24:02 -05005257 .remove = cas_remove_one,
David S. Miller1f26dac2005-09-27 15:24:13 -07005258#ifdef CONFIG_PM
5259 .suspend = cas_suspend,
5260 .resume = cas_resume
5261#endif
5262};
5263
5264static int __init cas_init(void)
5265{
5266 if (linkdown_timeout > 0)
5267 link_transition_timeout = linkdown_timeout * HZ;
5268 else
5269 link_transition_timeout = 0;
5270
Jeff Garzik29917622006-08-19 17:48:59 -04005271 return pci_register_driver(&cas_driver);
David S. Miller1f26dac2005-09-27 15:24:13 -07005272}
5273
5274static void __exit cas_cleanup(void)
5275{
5276 pci_unregister_driver(&cas_driver);
5277}
5278
5279module_init(cas_init);
5280module_exit(cas_cleanup);